Check if a string is a number or a letter in Python

avatar

Borislav Hadzhiev

Last updated: Sep 19, 2022

banner

Photo from Unsplash

Check if a string is a number or a letter in Python #

To check if a string is a number or a letter:

  1. Use the str.isnumeric() method to check if the string contains only numbers.
  2. Use the str.isalpha() method to check if the string contains only letters.
main.py
# โœ… check if a string is a number print('5'.isnumeric()) # ๐Ÿ‘‰๏ธ True print('X'.isnumeric()) # ๐Ÿ‘‰๏ธ False # โœ… check if a string is a letter print('B'.isalpha()) # ๐Ÿ‘‰๏ธ True print('5'.isalpha()) # ๐Ÿ‘‰๏ธ False # โœ… check if a string contains only letters and numbers print('bobby123'.isalnum()) # ๐Ÿ‘‰๏ธ True print('bobbyhadz.com'.isalnum()) # ๐Ÿ‘‰๏ธ False my_str = '573' if my_str.isnumeric(): # ๐Ÿ‘‡๏ธ this runs print('The string is a number') elif my_str.isalpha(): print('The string contains only letters')

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).

Use a try/except statement if you need to check if a string is a valid integer or a valid floating-point number.
main.py
# โœ… Check if a string is a valid integer def is_integer(string): try: int(string) except ValueError: return False return True print(is_integer('359')) # ๐Ÿ‘‰๏ธ True print(is_integer('-359')) # ๐Ÿ‘‰๏ธ True print(is_integer('3.59')) # ๐Ÿ‘‰๏ธ False print(is_integer('3x5')) # ๐Ÿ‘‰๏ธ False # ---------------------------------------------- # โœ… Check if a string is a valid float def is_float(string): try: float(string) except ValueError: return False return True print(is_float('359')) # ๐Ÿ‘‰๏ธ True print(is_float('-359')) # ๐Ÿ‘‰๏ธ True print(is_float('3.59')) # ๐Ÿ‘‰๏ธ True print(is_float('3x5')) # ๐Ÿ‘‰๏ธ False
If converting the string to an integer or float fails, the except block runs where we handle the ValueError by returning False from the function.

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

main.py
print('BOBBY'.isalpha()) # ๐Ÿ‘‰๏ธ True # ๐Ÿ‘‡๏ธ contains space print('BOBBY HADZ'.isalpha()) # ๐Ÿ‘‰๏ธ False

The str.isalpha method considers alphabetic characters the ones that are defined in the Unicode character database as "Letter".

If you need to check if the string is an ASCII (a-z A-Z) letter, use the string module and the in operator.

main.py
import string ascii_letters = string.ascii_letters # ๐Ÿ‘‡๏ธ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ print(ascii_letters) my_str = 'B' if my_str in ascii_letters: # ๐Ÿ‘‡๏ธ this runs print('The string is an ASCII letter') else: print('The string is NOT an ASCII letter') print('B' in ascii_letters) # ๐Ÿ‘‰๏ธ True print('ะค' in ascii_letters) # ๐Ÿ‘‰๏ธ False

The string.ascii_letters attribute returns a string containing the lowercase and uppercase ASCII letters.

The in operator tests for membership. For example, x in s evaluates to True if x is a member of s, otherwise it evaluates to False.

If you need to check if the string contains only numbers and letters, use the str.isalnum() method.

The str.isalnum method returns True if all characters in the string are alphanumeric and the string contains at least one character, otherwise the method returns False.

main.py
print('bobby123'.isalnum()) # ๐Ÿ‘‰๏ธ True # ๐Ÿ‘‡๏ธ contains space print('bobby hadz'.isalnum()) # ๐Ÿ‘‰๏ธ False
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.

Copyright ยฉ 2022 Borislav Hadzhiev