Python String isalnum()

Python String isalnum()

The isalnum() method returns True if all characters in the string are alphanumeric (either alphabets or numbers). If not, it returns False.

The syntax of isalnum() is:

string.isalnum()

isalnum() Parameters

The isalnum() doesn't take any parameters.


Return Value from isalnum()

The isalnum() returns:

  • True if all characters in the string are alphanumeric
  • False if at least one character is not alphanumeric

Example 1: Working of isalnum()

name = "M234onica"
print(name.isalnum())

# contains whitespace
name = "M3onica Gell22er "
print(name.isalnum())

name = "Mo3nicaGell22er"
print(name.isalnum())

name = "133"
print(name.isalnum())

When you run the program, the output will be:

True
False
True
True

Example 1: Working of isalnum()

name = "M0n1caG3ll3r"

if name.isalnum() == True:
   print("All characters of string (name) are alphanumeric.")
else:
    print("All characters are not alphanumeric.")

When you run the program, the output will be:

All characters of string (name) are alphanumeric.

Checkout these related String methods as well: