Open In App

Python String isalnum() Method

Last Updated : 21 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The isalnum() method is a string function in Python that checks if all characters in the given string are alphanumeric. If every character is either a letter or a number, isalnum() returns True. Otherwise, it returns False.

For Example:

Python
s = "Python123"
res = s.isalnum()
print(res)

Output
True

Explanation: In this example, s contains only letters and numbers, so isalnum() returns True.

Syntax

s.isalnum()

Parameters:

  • This method does not accept any parameters.

Return Type:

  • Returns True if all characters in the string are alphanumeric.
  • Returns False if there are any non-alphanumeric characters.

Examples of isalnum() Method

Example 1: Empty String

An empty string will return False since there are no alphanumeric characters.

Python
s = ""
res = s.isalnum()
print(res)

Output
False

Example 2: String with Only Digits

The method will also return True for a string with only numeric characters.

Python
s = "123456"
res = s.isalnum()
print(res)

Output
True

Example 3: String with Only Letters

Similarly, a string containing only letters will return True.

Python
s = "Python"
res = s.isalnum()
print(res)

Output
True

Example 4: String with Non-Alphanumeric Characters

If the string contains a space and an exclamation mark, so isalnum() returns False.

Python
s = "Python 123!"
res = s.isalnum()
print(res)

Output
False

Explanation: in this code, isalnum() returns False because the string s contains space " " and exclamation mark (!) which are neither numbers nor alphabets.


Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy