Python String isalnum() Method
By Lenin Mishra
If you prefer to watch Youtube videos over reading blogs, check out our video on Python strings here.
Python String isalnum()
method
The isalnum()
method in Python checks if all characters in the string are alphanumeric.
Alphanumeric characters - Alphabet letters (a-z) and numbers (0-9)
Syntax of isalnum()
method
string.isalnum()
Returns True or False
Example 1
Code
s1 = "Pylenin1992"
print(s1.isalnum())
Output
True
Example 2 - Check for a sentence
Code
s1 = "Pylenin was born in 1992"
print(s1.isalnum())
Output
False
Example 3 - Check for special characters
Code
s1 = "pylenin1992@gmail.com"
print(s1.isalnum())
Output
False
As you can see, @
is not an alphanumeric character.
Let’s check for random characters.
Code
s1 = "!#%&?"
print(s1.isalnum())
Output
False
Check out other commonly used Python string methods.
Related Articles
- How to create a string in Python?
- How to access characters in a Python string?
- How to replace characters in a string in Python?
- How to concatenate strings in Python?
- How to iterate through a string in Python?
- Check if a Substring is Present in a Given String in Python
- Escape sequences in Python String
- Python String Formatting - The Definitive Guide