Check if string contains only alphanumeric characters in Python

avatar

Borislav Hadzhiev

Last updated: Sep 19, 2022

banner

Photo from Unsplash

Check if string contains only alphanumeric characters in Python #

Use the str.isalnum() method to check if a string contains only alphanumeric characters, e.g. if string.isalnum():. The str.isalnum() method will return True if the string contains only alphanumeric characters, otherwise False is returned.

main.py
def only_alphanumeric(string): return string.isalnum() print(only_alphanumeric('bobby305')) # 👉️ True print(only_alphanumeric('bobby.hadz')) # 👉️ False print(only_alphanumeric('')) # 👉️ False print(only_alphanumeric(' ')) # 👉️ False if only_alphanumeric('bobby246'): # 👇️ this runs print('The string contains only alphanumeric charcaters') else: print('The string does NOT contain only alphanumeric characters')
If you need a more flexible regex solution, scroll down to the next subheading.

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('bobby305'.isalnum()) # 👉️ True # 👇️ contains space print('bobby hadz'.isalnum()) # 👉️ False

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

Check if string contains only alphanumeric characters using re.match() #

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

main.py
import re def only_alphanumeric(string): return bool(re.match(r'^[a-zA-Z0-9]+$', string)) if only_alphanumeric('bobby246'): # 👇️ this runs print('The string contains only alphanumeric characters') else: print('The string does NOT contain only alphanumeric characters') print(only_alphanumeric('bobby246')) # 👉️ True print(only_alphanumeric('')) # 👉️ False print(only_alphanumeric(' ')) # 👉️ False print(only_alphanumeric('bobby_hadz')) # 👉️ False print(only_alphanumeric('bobbyhadz.com')) # 👉️ False

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.

We used the bool() class to convert the match object or None value to a boolean.

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

main.py
import re def only_alphanumeric(string): return bool(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 adjust the regular expression to allow for more characters by adding them between the square brackets.

Here is an example that checks if the string contains only letters, numbers, underscores, hyphens and spaces.

main.py
import re def only_alphanumeric(string): return bool(re.match(r'^[a-zA-Z0-9 _-]+$', string)) print(only_alphanumeric('bobby-hadz_307')) # 👉️ True print(only_alphanumeric('bobby hadz 307')) # 👉️ True print(only_alphanumeric('bobby.hadz.307')) # 👉️ False

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.