Python String isalnum: The Complete Guide

Python isalnum() function in Python language checks whether all the characters in a given string are alphanumeric.

Python String isalnum()

Python string isalnum() is a built-in method used to check whether the given string consists of only alphabets and numbers. The isalnum() function returns True if a string is alphanumeric. Otherwise, it returns False.

In other words, we can say that it checks if the characters present in the string are alphanumeric or not. The isalnum() is the perfect function to check if the string contains the alphanumeric characters or not. It solves the problem.

Syntax

string.isalnum()

Python string isalnum() doesn’t take any parameters.

Return value

Python isalnum() returns:

  1. True if all the characters in the string are alphanumeric.
  2. False if at least one character is not alphanumeric.

Write the program to show a mechanism of isalnum().

See the following code.

# app.py

string = "HelloWorld123"
string2 = "Hello!-World!-123..."
print("Output for the string which is alphanumeric:")
print(string.isalnum())
print("Output for the string which is not alphanumeric:")
print(string2.isalnum())

Output

➜  pyt python3 app.py
Output for the string which is alphanumeric:
True
Output for the string which is not alphanumeric:
False
➜  pyt

Write a program to check multiple strings if they are alphanumeric.

See the following code.

# app.py

string = "This world is a strange place!"
string2 = "People over here are not awesome!"

if(string.isalnum() == True and string2.isalnum() == True):
    print("This world is a strange place to live and people over here are not awesome!")
else:
    print("This world is not a strange place to live and people over here are awesome!")

Output

➜  pyt python3 app.py
This world is not a strange place to live and people over here are awesome!
➜  pyt

These (space)!#%&? characters are not alphanumeric.

See the following code example.

# app.py

string = "Krunal#$110470116021"

if(string.isalnum() == True):
    print("The string is alphanumeric!")
else:
    print("The string is not alphanumeric")

Output

➜  pyt python3 app.py
The string is not alphanumeric
➜  pyt

Printing all Alphanumeric characters in Python

We can use the Unicode module to check if the character is alphanumeric or not. Here is the program to print all the alphanumeric Unicode characters.

See the following code.

# app.py

import unicodedata

count = 0
for cdpt in range(2 ** 16):
    ch = chr(cdpt)
    if ch.isalnum():
        print(u'{:04x}: {} ({})'.format(
            cdpt, ch, unicodedata.name(ch, 'UNNAMED')))
        count = count + 1
print(f'Total Number of Alphanumeric Unicode Characters = {count}')

Output

ffbd: ᄑ (HALFWIDTH HANGUL LETTER PHIEUPH)
ffbe: ᄒ (HALFWIDTH HANGUL LETTER HIEUH)
ffc2: ᅡ (HALFWIDTH HANGUL LETTER A)
ffc3: ᅢ (HALFWIDTH HANGUL LETTER AE)
ffc4: ᅣ (HALFWIDTH HANGUL LETTER YA)
ffc5: ᅤ (HALFWIDTH HANGUL LETTER YAE)
ffc6: ᅥ (HALFWIDTH HANGUL LETTER EO)
ffc7: ᅦ (HALFWIDTH HANGUL LETTER E)
ffca: ᅧ (HALFWIDTH HANGUL LETTER YEO)
ffcb: ᅨ (HALFWIDTH HANGUL LETTER YE)
ffcc: ᅩ (HALFWIDTH HANGUL LETTER O)
ffcd: ᅪ (HALFWIDTH HANGUL LETTER WA)
ffce: ᅫ (HALFWIDTH HANGUL LETTER WAE)
ffcf: ᅬ (HALFWIDTH HANGUL LETTER OE)
ffd2: ᅭ (HALFWIDTH HANGUL LETTER YO)
ffd3: ᅮ (HALFWIDTH HANGUL LETTER U)
ffd4: ᅯ (HALFWIDTH HANGUL LETTER WEO)
ffd5: ᅰ (HALFWIDTH HANGUL LETTER WE)
ffd6: ᅱ (HALFWIDTH HANGUL LETTER WI)
ffd7: ᅲ (HALFWIDTH HANGUL LETTER YU)
ffda: ᅳ (HALFWIDTH HANGUL LETTER EU)
ffdb: ᅴ (HALFWIDTH HANGUL LETTER YI)
ffdc: ᅵ (HALFWIDTH HANGUL LETTER I)
Total Number of Alphanumeric Unicode Characters = 49473
➜  pyt

That’s it for this tutorial.

Related Posts

Python string isdigit()

Python string encode()

Python string isdecimal()

Python dictionary to a string

Python string to list

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.