String method isalnum() in Python
The string method isalnum() used to determine whether a string is alpha numeric. It returns boolean value True, if a string is alphanumeric, otherwise False. An alphanumeric includes latter (a-z) and numbers (0-9).
Syntax
 1 <string>.isalnum()
string : A source string to determine whether a string is alphanumeric or not
Note : If a string include latter's that can be (space)!#%&? etc, then a string is not alphanumeric.
String method isalnum()
 1 msg1 = "Hello@123";
 2 msg2 = "Hello123";
 3 
 4 # validate for alphanumeric
 5 b1 = msg1.isalnum()
 6 b2 = msg2.isalnum()
 7 
 8 # print result
 9 print('String includes only alphanumeric :', b1)
 10 print('String includes only alphanumeric :', b2)
In the above example, a string variables are define and initialize with alphanumeric letters. The first string include non alphanumeric character. A isalnum() method is called for string variables which returns boolean value. The first string is non alphanumeric numeric while second string is alphanumeric. Hence, the first print statement False and second prints True.
Output
 1 String includes only alphanumeric : False
 2 String includes only alphanumeric : True

Example 2

Example 2
 1 msg1 = "Hello world";
 2 msg2 = "Helloworld";
 3 
 4 # validate for alphanumeric
 5 b1 = msg1.isalnum()
 6 b2 = msg2.isalnum()
 7 
 8 # print result
 9 print('String includes only alphanumeric :', b1)
 10 print('String includes only alphanumeric :', b2)
In the above example, a strings are define and isalnum() method called that validates whether a string includes alpha numeric value only. As first string includes white space, it returns false, while second method call return boolean value true.
Output
 1 String includes only alphanumeric : False
 2 String includes only alphanumeric : True
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us