isalnum function in python
In this post, you will learn about the isalnum() function in the Python programming language.
The Python string method isalnum() returns True if all characters in the string are alphanumeric (either alphabets or numbers). If not, it returns False. If the string is empty, then isalnum() returns False.
Syntax-str.isalnum()
Python isalnum() function examples
str1 = 'etutorialspoint2022'
print(str1.isalnum())
True
str1 = 'etutorialspoint 2022'
print(str1.isalnum())
False
str1 = ''
print(str1.isalnum())
False
str1 = 'etutorialspoint\\ 2022'
print(str1.isalnum())
False
str1 = 'etutorialspoint.2022'
print(str1.isalnum())
False
str1 = '#etutorialspoint2022'
print(str1.isalnum())
False
Uses of isalnum() in if…else statement
The isalnum() method can be use along with the if..else statements. The following example demonstrates this-
text = "etp2022"
if text.isalnum():
print("Text is alphanumeric.")
else:
print("Text is not alphanumeric.")
Output of the above code:
Text is alphanumeric.
text = "@etutorialspoint"
if text.isalnum():
print("Text is alphanumeric.")
else:
print("Text is not alphanumeric.")
Output of the above code:
Text is not alphanumeric.
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary