Python String isalnum() method
The isalnum() method is used to check that all the characters of a non-empty string are alphanumeric and there is at least one character in the string. This method returns True if the given string only has alphanumeric characters and the length of the string is a minimum of one character.
Syntax:
This method accepts no parameter.
Return Value:True | If the given string only has alphanumeric characters. |
False | If the given string has any non-alphanumeric character. |
# Python example of the isalnum() method
website = "tutorialsnation"
print(website. isalnum ()) #return True
website = "tutorials nation"
print(website. isalnum ()) #return False it contains white space
website = "@tutorialsnation"
print(website. isalnum ()) #return False it contains special character
Output:
Alphanumeric means either letters or number or both and white spaces are not alphanumeric
string = "Tutorials Nation"
print(string.isalnum()) # this returns 'False'
string = "TutotialsNation"
print(string.isalnum()) # this returns 'True'
Output:
Here is an example of removing all non-alphanumeric elements from a list.
pythonlist=['Mercury','$$','500','Venus','%','^^','90']
for el in pythonlist:
if el.isalnum():
print(w)
Output:
If you want to print all non-alphanumeric elements from the list.
pythonlist=['Mercury','$$','500','Venus','%','^^','90']
for el in pythonlist:
if not el.isalnum():
print(el)
Output: