Python String isalnum() Method

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

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.

Example

Let us take some examples to understand python's isalnum() method.

  1. When the calling string does not contain spaces:

Output:

  1. When the calling string contains spaces:

Output:

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() function 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() Method 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 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:

Password Validation

Validates if a password contains at least 1 letter and 1 digit and has a minimum length of 8 characters.

Output:

Remove Punctuation from a String

Removes all Punctuation characters from a given string using the isalnum() method.

Output

This code iterates through the Unicode codepoints (up to 65535) and checks if each character is alphanumeric using the isalnum() method. If a character is alphanumeric, it prints its codepoint, character, and name using Unicodedata.name(). Finally, it prints the total count of alphanumeric Unicode characters found.

Output

Conclusion

  • The isalnum function in python is used to check whether all the string characters are alphabets (a-z or A-Z) or numbers (0-9).
  • The syntax of the isalnum function in python is: string.isalnum().
  • The isalnum in python returns a boolean value.
  • 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.

See Also