How to Check if a String is Number in Python?

Spread the love

Problem –

You want to check whether a string is a number in python.

Solution –

To check if a string is number use the isdigit() method.

In [1]: string1 = '123456'

In [2]: string1.isdigit()
Out[2]: True

In [3]: string2 = '123 456'

In [4]: string2.isdigit()
Out[4]: False

In [5]: string3 = 'python3'

In [6]: string3.isdigit()
Out[6]: False

Rating: 1 out of 5.

Leave a Reply