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