Python String isalnum()

Python string isalnum() method is a built-in function that returns True if all the characters in a string are alphanumeric. Otherwise, it returns false.

Example of characters that are not alphanumeric: (space)!#%&? etc.

What is Alphanumeric?

The alphanumeric is a string that consists of letters and numbers and often other symbols (such as punctuation marks and mathematical symbols).

isalnum() Syntax

The syntax of isalnum() method is:

string.isalnum()

isalnum() Parameters

The isalnum() method doesn’t take any parameters.

 isalnum() Return value

The isalnum() method returns:

  • True if all the characters in a string are either letters or numbers
  • False if one or more characters are not alphanumeric.

Example 1: Python string isalnum() working examples

# string is Alphanumeric 
text1 = "Python3"
print(text1.isalnum())

# string is Alphanumeric
text2= "abc123def456"
print(text2.isalnum())

# only alphabets and returns true
text3= "Itsmycode"
print(text3.isalnum())

# only numbers and returns true
text4="1234567"
print(text4.isalnum())

# Contains space returns false
text5= "Python Tutorials123"
print(text5.isalnum())

Output

True
True
True
True
False

Example 2: Program to check if the string is alphanumeric

# Alphanumeric string
text1 = "Python123Tutorials"
if(text1.isalnum()):
    print("String is Alphanumeric")
else:
    print("String is not Alphanumeric")

# Non Alphanumeric String
text2= "hello World"
if(text2.isalnum()):
    print("String is Alphanumeric")
else:
    print("String is not Alphanumeric")

Output

String is Alphanumeric
String is not Alphanumeric
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Remove Newline From String

Python Remove Newline From String

Table of Contents Hide Python Remove Newline From StringUsing strip() method to remove the newline character from a stringUsing replace() method to remove newlines from a stringUsing regex to remove…
View Post
Python List Insert()

Python List insert()

Table of Contents Hide Syntax of List insert() insert() ParametersReturn Value from List insert()Example 1: Inserting an Element to the ListExample 2: Inserting an element at the start and end of…
View Post
Nxnxn Matrix Python

nxnxn matrix python

Table of Contents Hide What is NxNxN?How to Create NxNxN Matrix in Python?Create NxN Matrix in Python 3 with Non Duplicating numbersCreate NxNxN matrix in Python using numpy In this…
View Post
Python String Capitalize()

Python String capitalize()

Table of Contents Hide capitalize() Syntaxcapitalize() ParameterReturn Value from capitalize()Example: Capitalize a string in Python Python string capitalize() method will convert the first letter in a string to uppercase and keep…
View Post