Check if a character is a Number in Python

avatar

Borislav Hadzhiev

Last updated: Sep 18, 2022

banner

Photo from Unsplash

Check if a character is a Number in Python #

Use the str.isnumeric() method to check if a character is a number, e.g. is_number = my_str[0].isnumeric(). The str.isnumeric() method will return True if the character is a number and False otherwise.

main.py
my_str = '12bo' # ✅ Check if character is a number is_number = my_str[0].isnumeric() print(is_number) # 👉️ True if is_number: # 👇️ this runs print('The character is a number') else: print('The character is NOT a number') is_number = my_str[-1].isnumeric() print(is_number) # 👉️ False # --------------------------------------- # ✅ Check if character is a digit print('123abc'[0].isdigit()) # 👉️ True print('123abc'[-1].isdigit()) # 👉️ False

The str.isnumeric method returns True if all characters in the string are numeric, and there is at least one character, otherwise False is returned.

main.py
print('5'.isnumeric()) # 👉️ True print('50'.isnumeric()) # 👉️ True print('-50'.isnumeric()) # 👉️ False print('3.14'.isnumeric()) # 👉️ False print('A'.isnumeric()) # 👉️ False

Notice that the str.isnumeric() method returns False for negative numbers (they contain a minus) and for floats (they contain a period).

You can access the character at a specific index when calling isnumeric().

main.py
my_str = '12bo' is_number = my_str[0].isnumeric() print(is_number) # 👉️ True is_number = my_str[-1].isnumeric() print(is_number) # 👉️ False
Python indexes are zero-based, so the first character in a string has an index of 0, and the last character has an index of -1 or len(my_str) - 1.

You can also use a for loop to iterate over the string and check if each character is a number.

main.py
my_str = '12bo' for char in my_str: # 1 True # 2 True # b False # o False print(char, char.isnumeric())

Alternatively, you can use the str.isdigit() method.

Check if a character is a Number using str.isdigit() #

Use the str.isdigit() method to check if a character in a string is a number, e.g. is_number = my_str[0].isdigit(). The str.isdigit() method will return True if the character is a number and False otherwise.

main.py
my_str = '12bo' is_number = my_str[0].isdigit() print(is_number) # 👉️ True if is_number: # 👇️ this runs print('The character is a number') else: print('The character is NOT a number')

The str.isdigit method returns True if all characters in the string are digits and there is at least 1 character, otherwise False is returned.

If you need to check if all characters in a string are numbers, use the all() function.

main.py
my_str = '1234' all_numbers = all(char.isdigit() for char in my_str) print(all_numbers) # 👉️ True if all_numbers: # 👇️ this runs print('All characters in the string are numbers') else: print('Not all characters in the string are numbers')
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use the str.isdigit() method to check if the current character is a number and return the result.

The all() built-in function takes an iterable as an argument and returns True if all elements in the iterable are truthy (or the iterable is empty).

The all() function returns True for empty strings.

If you need to handle this scenario, check for the string's length.

main.py
my_str = '' all_numbers = (len(my_str) > 0 and all(char.isdigit() for char in my_str)) print(all_numbers) # 👉️ False

We used the boolean and operator, so for the expression to return True both conditions have to be met.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.