The Python String isalnum() Method tests whether or not all characters are alphanumeric. A letter(a-z) or a number(0-9) character is classified as alphanumeric. Special chars, like spaces, are not permitted.
The syntax of the method is: str.isalnum()
Key Points:
• Return Type: Boolean i.e. True or False
• Parametric Values: There are no isalpha() function parameters.
• There should be no spaces in the string.
• False returns the null string as well.
• Not case responsive, i.e., value return does not depend on string subject.
We’ll be covering the following topics in this tutorial:
Return Value from isalnum()
The isalnum() returns:
• If all characters are alphanumeric, the isalnum() method returns True, which implies alphabet letter (a-z and number (0-9).
• If the string is empty, isalnum() returns False.
Below is the python program to demonstrate the isalnum() function:
str = "TheBestLearningResourceforOnlineEducation"; print (str.isalnum()) str = "The Best Learning Resource for Online Education"; print (str.isalnum())
When we run above the program, the outcome is as follows:
True
False
Working of isalnum()
username = input("Choose a username:") if username.isalnum() == True: print("Your new username is ", username) else: print("This username is invalid.")
When we run above the program, the outcome is as follows:
Choose a username:DThakur
Your new username is DThakur
Below are several other functions that we can use to work with string in Python 3