Python string isalnum() Method

In this tutorial, We will see the python string isalnum() method with examples.

isalnum() Method

The isalnum() method is one of the methods in a python string. In isalnum, alnum is a combination of 2 words.  al and num, here al means alphabets(a-z, A-Z) and num means numbers(0-9).

Python string isalnum() method checks all the characters in the string are alphabets(a-z, A-Z) or digit(0-9).

  • Return True, If all the characters are alphabets(a-z , A-Z) or digit(0-9).
  • Return False, If all the characters are not alphabets(a-z , A-Z) or digit(0-9).
Syntax of Python string isalnum() method

This is the syntax of the python string isalnum() method.

Example of the Python string isalnum() method
#Example 1 

text = "windows12"
#check the text contains only alphabets or digit.
#output : True
#Example 2

text = "python3"

#output: True

#Exmaple 3

message = "python"
msg = message.isalnum()

#output: False

messg = "!23python"
messg1 = messg.isalnum()

# ! is not a alphabet or digit.
#output: False

Program: Take a string from the user and check all the characters in a string are alphabets or digits.

message = input("Enter phone number")
msg = message.isalpha()

Related Posts