Check if string contains only Letters and Numbers in Python

avatar

Borislav Hadzhiev

Last updated: Sep 18, 2022

banner

Photo from Unsplash

Check if string contains only Letters and Numbers in Python #

Use the str.isalnum() method to check if a string contains only letters and numbers, e.g. if string.isalnum():. The str.isalnum() method returns True if all characters in the string are alphanumeric and the string contains at least one character, otherwise False is returned.

main.py
def is_alphanumeric(string): return string.isalnum() if is_alphanumeric('bobby123'): # 👇️ this runs print('The string contains only letters and numbers') else: print('The string does NOT contain only letters and numbers') print(is_alphanumeric('bobby')) # 👉️ True print(is_alphanumeric('bobby123')) # 👉️ True print(is_alphanumeric('bobbyhadz.com')) # 👉️ False print(is_alphanumeric('')) # 👉️ False print(is_alphanumeric(' ')) # 👉️ False
If you're looking for a more flexible regex solution, scroll down to the next subheading.

We used the str.isalnum() method to check if a string contains only letters and numbers.

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

Alternatively, you can use the re.match() method.

Check if string contains only Letters and Numbers using re.match() #

Use the re.match() method to check if a string contains only letters and numbers, e.g. re.match(r'^[a-zA-Z0-9]+$', string). The re.match() method will return a match object if the string is alphanumeric, otherwise None is returned.

main.py
import re def is_alphanumeric(string): return re.match(r'^[a-zA-Z0-9]+$', string) if is_alphanumeric('bobby123'): # 👇️ this runs print('The string contains only letters and numbers') else: print('The string does NOT contain only letters and numbers') # 👇️ <re.Match object; span=(0, 5), match='bobby'> print(is_alphanumeric('bobby')) # 👇️ <re.Match object; span=(0, 8), match='bobby123'> print(is_alphanumeric('bobby123')) print(is_alphanumeric('bobbyhadz.com')) # 👉️ None print(is_alphanumeric('')) # 👉️ None print(is_alphanumeric(' ')) # 👉️ None

The re.match method returns a match object if the provided regular expression is matched in the string.

The match() method returns None if the string doesn't match the regex pattern.

The first argument we passed to the re.match() method is a regex.

main.py
import re def is_alphanumeric(string): return re.match(r'^[a-zA-Z0-9]+$', string)

The square brackets [] are used to indicate a set of characters.

The a-z and A-Z characters represent lowercase and uppercase ranges of letters.

The 0-9 characters match the digits in the range.

The caret ^ matches the start of the string and the dollar sign $ matches the end of the string.

The plus + causes the regular expression to match 1 or more repetitions of the preceding character (the letter and number ranges).

You can also tweak the regular expression if you have to. Here is an example that checks if the string contains only letters, numbers, underscores and hyphens.

main.py
import re def is_alphanumeric(string): return re.match(r'^[a-zA-Z0-9_-]+$', string) # # 👇️ <re.Match object; span=(0, 5), match='bobby'> print(is_alphanumeric('bobby_hadz-123'))

If you need to tweak the regular expression, add the characters you want to match between the square brackets.

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.