Home » Python

Python String | isalnum() and isalpha() Methods with Examples

Python Strings | isalnum() and isalpha() Methods with Example, these are the in-built methods. Here, we will learn about isalnum() and isalpha() methods of Python String.
Submitted by IncludeHelp, on July 08, 2018

1 ) isalpha() Method

isalpha() method returns true if all characters of a string are only alphabets (between ‘A’ to ‘Z’ and between ‘a’ to ‘z’). If there is any non alphabet character in the string, method returns false.

Syntax:

    string.isalpha()

Parameter: None

Return type:

  • true - if all characters of the string are alphabets.
  • false - if any of the character of the string is not an alphabet (like number, space, special characters).

Example:

# str1 with only alphabets
str1 = "Helloworld"

# str2 with alphabets, space and special character
str2 = "Hello world!"

# str3 with alphabets, space and numbers
str3 = "Hello world 100"

# check whether string contains only alphabets or not
print str1.isalpha ()
print str2.isalpha ()
print str3.isalpha ()

Output

    True
    False
    False

2) isalnum() Method

isalnum() method returns true if all characters of a string are either alphabets (between ‘A’ to ‘Z’ and between ‘a’ to ‘z’) or numbers (0 to 9) or both, otherwise method returns false.

Syntax:

    string.isalnum()

Parameter: None

Return type:

  • true - if all characters of the string are alphabets or/and numbers.
  • false - if any of the character of the string is not either an alphabet or a number.

Example:

# str1 with alphabets and numbers
str1 = "Helloworld123"

# str2 with only alphabets
str2 = "Helloworld"

# str3 with numbers only
str3 = "12345"

# str4 with alpa numeric characters and space 
str4 = "Amit Shukla 21"

# check whether string contains only alphabets or not
print str1.isalnum ()
print str2.isalnum ()
print str3.isalnum ()
print str4.isalnum ()

Output

    True
    True
    True
    False



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.