Python String isalnum() method
The string.isalnum () method in Python returns a boolean value true if all characters in the string are alphanumeric else for any other value it returns false.
Syntax
1 2 3 |
String.isalnum() |
Parameter
NA
Return
This method returns a Boolean value True if all the given characters are alphanumeric.
Example 1
1 2 3 4 5 6 7 8 9 10 |
# Python programing explaining # the isalnum() method # initializing a alhanumeric value without validating any space strVal = "July2019"; print (strVal.isalnum()) # passing string value strVal = "Hello World !!"; print (strVal.isalnum()) |
Output
1 2 3 4 |
True False |
Example 2
1 2 3 4 5 6 7 8 9 10 11 |
# Python programing explaining # the isalnum() method # initializing a string value strVal = "M0n1caG3ll3r" # validating if and else condition if strVal.isalnum() == True: print("All characters of specified string are alphanumeric.") else: print("All the characters are not alphanumeric.") |
Output
1 2 3 |
All characters of specified string are alphanumeric. |
Example 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# Python programing explaining # the isalnum() method # initializing a string value # will return true char1 = "M234onica" print(char1.isalnum()) # contains whitespace # will return False as it contains space char2 = "M3onica Gell22er " print(char2.isalnum()) #passing special characters # will return false as it contains special characters char3 = "!Mo3nica*$Gell22er" print(char3.isalnum()) # passing all integers # will return true as it is alphanumeric char4 = "133" print(char4.isalnum()) |
Output
1 2 3 4 5 6 |
True False False True |