isalnum() in Python | Python String isalnum() Method
Learn via video course
Overview
The isalnum in python programming is used to check whether all the characters of the string are either alphabets (a-z or A-Z) or numbers (0-9). The isalnum method does not take any parameter and returns a boolean value (either True or False) after checking the string.
Syntax of isalnum() in Python
The syntax of the isalnum in python is very simple and does not take any parameter. We need to call or invoke the isalnum function in python on a string.
Here string is an immutable sequence data type invoking the isalnum() method. For example: abcd123
Parameters of isalnum() in Python
The isalnum in python is called from a string. The isalnum in python does not take any parameter. It simply checks that the method invoking string contains either alphabets or numbers.
Return Value of isalnum() in Python
The isalnum in python returns a boolean value after checking the string is either alphabets or numbers.
Note:
Alphanumeric means the characters of the string are either alphabets (letters) or numbers.
True
The isalnum in python returns True if the entire string is alphanumeric (neither alphabets not numbers).
False
The isalnum in python returns False if one or more characters are neither alphabets nor numbers (not alphanumeric).
Exceptions of isalnum() in Python
Usually, the isalnum in python does not raise an error if we use the correct syntax.
Even if the string calling the isalnum() method is alphanumeric but contains white spaces (or empty spaces), the method returns False.
Check the example section below for a better understanding.
Example
Let us take some examples to understand python's isalnum() method.
- When the calling string does not contain spaces:
Output:
- When the calling string contains spaces:
Output:
What is isalnum() in Python?
The isalnum in python is a string method that is used to check whether all the string characters are either alphabets (a-z or A-Z) or numbers (0-9).
The isalnum in python returns a boolean value. If all the characters of the calling string are alphanumeric (either alphabets or numbers), the isalnum() method returns True. On the other hand, if the calling string contains neither alphabets nor numbers, then the isalnum() method returns False.
Example:
- If the string is abcde12345, the isalnum() method return True.
- If the string is abcde or 12345, the isalnum() method return True.
- If the string is /$#%., the isalnum() method return False.
Note:
If the string calling the isalnum() method is alphanumeric but contains white spaces (or empty spaces), then the method returns False.
Example of isalnum() in Python
So far, we have learned all about the isalnum in python. Now let us take some examples and understand the method better.
Working of isalnum()
The isalnum in python returns a boolean value. If all the characters of the calling string are alphanumeric (either alphabets or numbers), the isalnum() method returns True, else returns False.
Output:
All Characters of String are Alphanumeric or not Alphanumeric
The isalnum in python return True if the calling string is alphanumeric. On the other hand, if the calling string contains neither alphabets nor numbers then the isalnum() method returns False.
The isalnum in python returns False if one or more characters are neither alphabets not numbers (not alphanumeric).
Output:
Conclusion
- The isalnum in python is used to check whether all the string characters are either alphabets (a-z or A-Z) or numbers (0-9).
- The syntax of the isalnum in python is: string.isalnum(). The isalnum in python is called from a string. It does not take any parameter.
- The isalnum in python returns a boolean value after checking the string is either alphabets or numbers.
- The isalnum in python returns True if the entire string is alphanumeric (neither alphabets nor numbers). Else, it returns False.
- If a string invoking the isalnum() method is alphanumeric (for example: ab cd) but contains white spaces (or empty spaces), then the method returns False.