Open In App
Related Articles

Python String isalnum() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Python String isalnum() method checks whether all the characters in a given string are either alphabet or numeric (alphanumeric) characters.

Python String isalnum() Method Syntax:

Syntax:  string_name.isalnum() 

Parameter:  isalnum() method takes no parameters 

Return: 

  • True: If all the characters are alphanumeric 
  • False: If one or more characters are not alphanumeric 

Time Complexity: O(1)

Auxiliary Space: O(1)

Python String isalnum() Method Example:

Python program to demonstrate the use of String isalnum() method

Python

# here a,b and c are characters and 1,2 and 3
# are numbers
string = "abc123"
print(string.isalnum())

                    

Output: 

True

Example 1: More examples on Python String isalnum() Method

Python3

string = "abc 123"
print(string, "is alphanumeric?", string.isalnum())
 
string = "abc_123"
print(string, "is alphanumeric?", string.isalnum())
 
string = "000"
print(string, "is alphanumeric?", string.isalnum())
 
string = "aaaa"
print(string, "is alphanumeric?", string.isalnum())

                    

Output:

abc 123 is alphanumeric? False
abc_123 is alphanumeric? False
000 is alphanumeric? True
aaaa is alphanumeric? True

Example 2: isalnum() in if…else Statement

We can also use Python String isalnum() Method along with if…else statements, to output custom messages

Python3

password = "user123456"
 
if password.isalnum():
    print("Password is alphanumeric.")
else:
    print("Password is not alphanumeric.")

                    

Output:

Password is alphanumeric.


Last Updated : 27 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads