Table of Contents
The isalnum() method returns True if all characters in the string are alphanumeric (either alphabets or numbers). If not, it returns False.
The syntax of isalnum() is:
string.isalnum()
1. isalnum() Parameters
The isalnum() doesn’t take any parameters.
2. Return Value from isalnum()
The isalnum() returns:
- True if all characters in the string are alphanumeric
- False if at least one character is not alphanumeric
3. Example 1: Working of isalnum()
name = "M234onica" print(name.isalnum()) # contains whitespace name = "M3onica Gell22er " print(name.isalnum()) name = "Mo3nicaGell22er" print(name.isalnum()) name = "133" print(name.isalnum())
Output
True False True True
4. Example 2: Working of isalnum()
name = "M0n1caG3ll3r"
if name.isalnum() == True:
print("All characters of string (name) are alphanumeric.")
else:
print("All characters are not alphanumeric.")
Output
All characters of string (name) are alphanumeric.
Checkout these related String methods as well:
- Python isalpha()
- Python isdigit()
Related posts:
Python Program to Display Powers of 2 Using Anonymous Function
Python Program to Generate a Random Number
Python List remove()
Python zip()
Python Program to Sort a Dictionary by Value
Python Program to Print the Fibonacci sequence
CharSequence vs. String in Java
Python super()
Python Program to Convert Kilometers to Miles
Python Sets
Python Set update()
Python id()
Python frozenset()
Python Dictionary setdefault()
Python Program to Slice Lists
Python Program to Split a List Into Evenly Sized Chunks
Python String casefold()
Python frozenset()
Python Program to Count the Number of Occurrence of a Character in String
Deep Learning with Python - A Hands-on Introduction - Nikhil Ketkar
Encode a String to UTF-8 in Java
Python Global Keyword
Python vars()
Python Program to Remove Punctuations From a String
Python str()
Python Set remove()
Python String count()
Python Program to Find the Square Root
Python bin()
Converting String to Stream of chars
Python Program to Iterate Over Dictionaries Using for Loop
Python __import__()