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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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).

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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

Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more