How To Check If A Character Is A Number In Python

To check if a character is a Number in Python, you can use the if-else statement, ord(), str.isdigit(), str.isnumeric() functions. Each of them also has different uses. Let’s see how to perform them.

Check If A Character Is A Number In Python

Using the if-else statement

You can use the if-else statement to check if a character is a Number in Python. To do that, simply you check if the character is greater or equal to ‘0’ and less or equal to ‘9’ or not. If True, the character is a Number. Otherwise, it is not a Number.

Look at the example below.

def is_number(character=str()):
    # Check if the character is a Number or not.
    if character >= '0' and character <= '9':
        print('{} is a Number.'.format(character))
    else:
        print('{} is not a Number.'.format(character))

# Check whether a character is a Number or not.
character = '1'
is_number(character)

# Try with another character.
character = 'a'
is_number(character)

Output

1 is a Number.
a is not a Number.

Using the ord() function

Besides the if-else statement, you can use the ord() function to check if a character is a Number in Python. It is used to get the ASCII value of the character. After that, you compare it with 0 and 9. If it is between 0 and 9, the character is a Number. Otherwise, it is not a Number.

Look at the example below.

def is_number(character=str()):
    # Get the ASCII value of the character.
    ascii_value = ord(character)

    # Check if the character is a Number or not.
    if ascii_value >= 48 and ascii_value <= 57:
        print('{} is a Number.'.format(character))
    else:
        print('{} is not a Number.'.format(character))

# Check whether a character is a Number or not.
character = '1'
is_number(character)

# Try with another character.
character = 'a'
is_number(character)

Output

1 is a Number.
a is not a Number.

Using the str.isdigit() function

In addition, you can use the isdigit() function instead. It is built in the String object to check if a character is a Number or not.

Look at the example below.

def is_number(character=str()):
    # Check if the character is a Number or not with the str.isdigit() function.
    if str.isdigit(character):
        print('{} is a Number.'.format(character))
    else:
        print('{} is not a Number.'.format(character))

# Check whether a character is a Number or not.
character = '1'
is_number(character)

# Try with another character.
character = 'a'
is_number(character)

Output

1 is a Number.
a is not a Number.

Using the str.isnumeric() function

Moreover, you can use the str.isnumeric() function to check if a character is a Number in Python. It is used like the str.isdigit() function above.

Look at the example below.

def is_number(character=str()):
    # Check if the character is a Number or not with the str.isnumeric() function.
    if str.isnumeric(character):
        print('{} is a Number.'.format(character))
    else:
        print('{} is not a Number.'.format(character))

# Check whether a character is a Number or not.
character = '1'
is_number(character)

# Try with another character.
character = 'a'
is_number(character)

Output

1 is a Number.
a is not a Number.

Summary

We have shown you how to check if a character is a Number in Python in 4 ways. Personally, you should use the str.isdigit() or str.isnumeric() functions to achieve the goal because of their simplicity. We hope this tutorial is helpful to you. Thanks!

Leave a Reply

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