Categories
Python Answers

How to check if string input is a number with Python?

Spread the love

Sometimes, we want to check if string input is a number with Python.

In this article, we’ll look at how to check if string input is a number with Python.

How to check if string input is a number with Python?

To check if string input is a number with Python, we can use the string’s isnumeric method.

For instance, we write:

a = input()
isnumeric = a.isnumeric()
print('is numeric' if isnumeric else 'not numeric')

We call input to get user input and assign the input value to a.

Then we call isnumeric on a to check if it’s numeric.

If it is, True is returned. Otherwise, it returns False.

Therefore, if we enter numbers, we see 'is numeric'. Otherwise, 'not numeric' is printed.

Conclusion

To check if string input is a number with Python, we can use the string’s isnumeric method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *