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
.
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”.
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.
Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
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
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.