
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
- Python Useful Resources
- Python - Questions and Answers
- Python - Quick Guide
- Python - Tools/Utilities
- Python - Useful Resources
- Python - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python String isalnum() Method
The python string isalnum() method is used to check whether the string consists of alphanumeric characters. This method returns true if all the characters in the input string are alphanumeric and there is at least one character. Otherwise, it returns false.
A character 'char' is considered to be alphanumeric if it returns true for all these functions: char.isalpha(), char.isdecimal(), char.isdigit(), or char.isnumeric(). That is, the character should be either lowercase alphabet, uppercase alphabet, a number/digit, or a decimal. Except these, the other categories are not considered to be alphanumeric.
Let us look into this function with more details in the following section.
Syntax
Following is the syntax for the python string isalnum() method −
str.isalnum()
Parameters
The python string isalnum() method does not contain any parameters.
Return Value
The python string isalnum() method returns true if all characters in the string are alphanumeric and there is at least one character and false otherwise.
Example
The lowercase, uppercase alphabets and numbers come under alphanumeric characters.
The following is an example of the python string isalnum() method. In this we are trying to find out whether the string "tutorial" is alpha numeric.
#!/usr/bin/python str = "tutorial" result=str.isalnum() print("Are all the characters of the string alphanumeric?", result)
On executing the above program, the following output is generated -
Are all the characters of the string alphanumeric? True
Example
Only the lowercase, uppercase alphabets and numbers come under alphanumeric characters. Other characters such as '!', '.', '/', etc.. are not alphanumeric characters. Even if the string containing alphabets has these kind of non-alphanumeric characters, the isalnum() function returns false.
#!/usr/bin/python str = "Hello!Welcome." result=str.isalnum() print("Are all the characters of the string alphanumeric?", result)
The following is the output obtained by executing the above program -
Are all the characters of the string alphanumeric? False
Example
Only the lowercase, uppercase alphabets and numbers come under alphanumeric characters. Other characters such as '!', '.', '/', '@', '#' etc. are not alphanumeric characters. When the isalnum() method is applied on such strings containing those characters, it returns false.
#!/usr/bin/python str = "#@%$" result=str.isalnum() print("Are all the characters of the string alphanumeric?", result)
The following output is obtained by executing the above program -
Are all the characters of the string alphanumeric? False
Example
Only the lowercase, uppercase alphabets and numbers come under alphanumeric characters. Other characters such as '!', '.', '/', etc.. are not alphanumeric characters.
#!/usr/bin/python str = "Aa12" result=str.isalnum() print("Are all the characters of the string alphanumeric?", result)
The above program, on executing, displays the following output -
Are all the characters of the string alphanumeric? True
Example
The lowercase alphabets, uppercase alphabets, and the numbers are considered as alphanumeric characters. Even the empty space " " is also not considered alphanumeric.
#!/usr/bin/python str = "1234567189 " result=str.isalnum() print("Are all the characters of the string alphanumeric?", result)
The output of the above program is displayed as follows -
Are all the characters of the string alphanumeric? False