Tutorial

Python String isalnum()

Published on August 3, 2022
author

Pankaj

Python String isalnum()

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 our products

About the authors
Default avatar
Pankaj

author

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.

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!

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.