3 Ways to check if a Character Is a Number in Python

Learn the three python methods for checking if a character is a number for input validation

check if a Character Is a Number in Python
check if a Character Is a Number in Python

The need to check if a character is a number could arise over several scenarios. For example, if you want to validate the user input, parse strings, sanitize data, e.g., CSV files, or improve the code readability. The methods shown in this guide will come in very handy when you find yourself in either of these scenarios.

In Python, there are multiple ways to check if a character is a number. This article concentrates on the isnumeric(), isdigit(), and the isdecimal() methods. These three methods work in a similar as they all return booleans.

Let's see how to use them.

Method 1: Using the isnumeric() method

isnumeric() is the first method that returns True if the character is numeric and False otherwise. For example:

char = '5'
if char.isnumeric():
    print(char, 'is a number')
else:
    print(char, 'is not a number')

This would print "5 is a number".

Note: isnumeric() only works for characters that are part of the Unicode numeric character category, which includes digits from various scripts (e.g., Arabic, Devanagari, Latin, etc.), as well as certain symbols (e.g., the full stop or period, comma, and hyphen-minus). It does not include characters like +, -, or. (decimal point).

Method 2: Using the isdigit() method

Another way to check if a character is a number is to use the isdigit() method, which also returns True if the character is a digit and False if it is not.

For example:

char = '5'
if char.isdigit():
    print(char, 'is a number')
else:
    print(char, 'is not a number')

This would also print "5 is a number".

Unlike isnumeric(), isdigit() only works for characters that are digits in the ASCII character set (i.e., 0 through 9). It does not include digits from other scripts or numeric characters like +, -, or ..

Method 3: Using the isdecimal() method

A third way to check if a character is a number is to use the isdecimal() method. This method will return True if the character is a decimal digit and False otherwise.

For example:

char = '5'
if char.isdecimal():
    print(char, 'is a number')
else:
    print(char, 'is not a number')

This would also print "5 is a number".

Like isdigit(), isdecimal() only works for characters that are decimal digits in the ASCII character set (i.e., 0 through 9). Therefore, it does not include digits from other scripts or numeric characters like +, -, or ..

You will notice that if you assign a character that is not numeric to the variable char. The output will print char is not a number. This occurs in all the three methods.

Conclusion

To summarize, checking if a char is a number is useful when working on big projects. It helps with user input validation, parsing strings, and code readability improvement, which makes it easier to maintain the code over time.

The isnumeric() method will return True if the character is part of the Unicode numeric character category. The isdigit(), on the other hand, returns True if the character is an ASCII digit. Finally, the isdecimal() method will return true if the character is an ASCII decimal digit.

Using the OR operator, these three methods can be used on their own or in combination to check for a broader range of numeric characters.

I hope this article was helpful. Share it with others who might find it interesting,

Thank you for reading :)

If you enjoy our content, please consider buying us a coffee to support our work:

Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.