Methods and Functions

Python String isalnum()

In Python, there are built-in functions for practically any action that can be performed on a string. When working with strings in Python, you may wish to verify whether the strings contain only letters, only numbers, or any alphanumeric characters. For example, a program that prompts the user to enter a username may want to ensure that the username is free of special characters. To resolve this issue Python isalnum() string function is used.

Python isalnum() function

Definition

  • The Python isalnum() String function examines a string for alphanumeric characters and returns True only if the string contains alphanumeric characters, i.e. either alphabet (a-z, A-Z) or numerals (0-9) or a mix of both.
  • Python isalnum() method returns True if all of the characters in the string are alphanumeric (either alphabets or numbers). If not, it returns False.

Python isalnum()

The Python isalnum() function returns a Boolean value depending on the alphanumeric characters in the string. Alphanumeric characters include characters that are either alphabets or numeric or a combination of both.

isalnum() Syntax

The syntax followed by Python isalnum() is as follows:

                    

string.isalnum()

isalnum() Parameters

Python String isalnum() function does not accept any parameters.

Return value of isalnum()

The isalnum() is responsible for returning a Boolean value:

  • True – if all the characters are alphanumeric in the string
  • False – if at least one character is not alphanumeric and also if there exist whitespaces between them.

Note – If the string is empty, the isalnum() function returns a False value.

Example 1: Working of isalnum()

Example

                    

# Python program to illustrate isalnum()
# contains all alphabets
txt = 'Programming'
print(txt.isalnum())

# contains all numbers
txt = '987532267'
print(txt.isalnum())

# contains alphabets and numbers
txt = 'We1c0m3'
print(txt.isalnum())

# contains whitespaces
txt = 'Hello Sam 100'
print(txt.isalnum())

Output

                    

True
True
True
False

Example 2: isalnum() in-depth

Example

                    

# Python program to illustrate isalnum()
txt = 'Hęlłø'
print(txt.isalnum())

# contains special characters
txt = 'Štūdeñt@123'
print(txt.isalnum())

# empty string
txt = ' '
print(txt.isalnum())

txt = 'A.B.C.10.20'
print(txt.isalnum())

Output

                    

True
False
False
False

In the first instance, the output is True. This is because these are all Alpha characters i.e. characters defined in the Unicode character database are known as alphabetic characters.

The second output is False. This is because special characters are not part of alphanumeric series. Also period (.) used in the 4th string instance is not an alphanumeric character. Hence the output False.

Example 3: Real-life applications of isalnum()

One applications of Python isalnum() as seen below can be used to identify the non-alphanumerics in a string.

Example 1

                    

def NonAlphanumeric(str):
        print('Non-Alphanumeric characters are: ')
        for i in str:
             if(i.isalnum() == False):
                  print(i)

NonAlphanumeric('@*Pythoñ Progråm%!')

Output

                    

Non-Alphanumeric characters are:
@
*

%
!

Another real-life application of Python string isalnum() can be for a username-password generating system. This is useful when we want to accept a password without any special characters which contain only alphabets and numerics.

Example 2

                    

password = 'Sh@rkdoom*'

if password.isalnum() == True:
    print('Password Accepted. All characters are alphanumeric')
else:
    print('Password Not Accepted. Few characters are not alphanumeric')

Output

                    

Password Not Accepted. Few characters are not alphanumeric

Frequently Asked Questions

Q1. What is isalnum in Python?

When working with strings in Python, you may wish to verify whether the strings contain only letters, only numbers, or any alphanumeric characters. To resolve this issue Python isalnum() string function is used. The Python isalnum() String function examines a string for alphanumeric characters and returns True only if the string contains alphanumeric characters, i.e. either alphabet (a-z, A-Z) or numerals (0-9) or a mix of both.

Q2. What does maketrans do in Python?

Python provides a string translate() function that is used to return each character in the string that is mapped to its matching character in the translation table. This translation table is created with the help of maketrans() method.

The string maketrans() method returns a translation mapping table that can be used by the translate() method. To put it simply, the maketrans() method is a static method that generates a one-to-one mapping of a character to its translation/replacement. Each character is given a Unicode representation for translation.

Q3. How do I check if the word is alphanumeric in Python?

Python provides a string class function called as isalnum() that is used to check if the string contains any alphanumeric characters or not and returns a Boolean value accordingly. Alphanumeric characters include characters that are either alphabets or numeric or a combination of both. The Python isalnum() String function examines a string for alphanumeric characters and returns True only if the string contains alphanumeric characters, i.e. either alphabet (a-z, A-Z) or numerals (0-9) or a mix of both.

Syntax:

                    

string.isalnum()

Example

                    

word = 'Hello123'
print(word.isalnum())

word = 'Hello Python'
print(word.isalnum())

word = 'G00dMornîng'
print(word.isalnum())

word = '123@@ABC'
print(word.isalnum())

Output

                    

True
False
True
False

Q4. What are the roles of these functions – isalnum(), isalpha()?

  • Python isalnum() – The isalnum() is a Python function that determines whether all of the characters in a string are alphanumeric. In other words, isalnum() determines whether a string contains solely letters, exclusively numbers, or both letters and numbers. If all of the characters are alphanumeric, the procedure returns True; otherwise, it returns False.
  • Python isalpha() – The isalpha() string method in Python is used to determine whether a string contains only alphabetical characters. In other words, isalpha() determines whether or not a string contains only letters. If every character in a string is a letter, the Python isalpha() method returns the Boolean value True; otherwise, it returns the Boolean value False.

Example

                    

# contains only letters
word = 'HelloPython'
print(word.isalnum())
print(word.isalpha())
print('')

# contains letters and numbers
word = 'Hello123'
print(word.isalnum())
print(word.isalpha())
print('')

# contains whitespaces
word = 'Good Morning'
print(word.isalnum())
print(word.isalpha())

Output

                    

True
True

True
False

False
False

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.