Tutorial

Python String isalnum()

Published on August 3, 2022
Default avatar

By Pankaj

Python String isalnum()

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Python string isalnum() function returns True if it’s made of alphanumeric characters only. A character is alphanumeric if it’s either an alpha or a number. If the string is empty, then isalnum() returns False. python string isalnum()

Python string isalnum() example

s = 'HelloWorld2019'
print(s.isalnum())

Output: True

s = 'Hello World 2019'

print(s.isalnum())

Output: False because whitespace is not an alphanumeric character.

s = ''
print(s.isalnum())

Output: False because it’s an empty string.

s='A.B'
print(s.isalnum())

s = '10.50'
print(s.isalnum())

Output:

False
False

The string contains period (.) which is not an alphanumeric character.

s = 'çåøÉ'
print(s.isalnum())

Output: True because all these are Alpha characters. Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”.

Printing all Alphanumeric characters in Python

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

import unicodedata

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

Output:

...
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 = 49567

I have provided only partial output because the number of alphanumeric unicode characters is huge.

You can checkout more Python examples from our GitHub Repository.

Reference: Official Documentation

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel