DEV Community

Srinivas Ramakrishna for ItsMyCode

Posted on • Originally published at itsmycode.com on

Python String isalpha()

ItsMyCode |

Python string isalpha() method is mainly used to check if the string is the alphabet or not. The isalpha() method returns true if all the characters in the string are alphabets. Otherwise, it returns false.

Python String isalpha()

Syntax – string.isalpha()

*Parameters – * None

*Return Value – * Returns True if all the characters in the string are alphabets; otherwise, it returns false.

Valid Alphabets

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz

Example 1 – Let’s take a look at few use cases of isalpha() method.

# Valid alphabet
text1= "HelloWorld"
print(text1.isalpha())

# contains whitespace
text2 = "Hello World"
print(text2.isalpha())

# contains Special Character
text3 = "HelloWorld!!"
print(text3.isalpha())

# contains Alphanumeric
text3 = "Hello123"
print(text3.isalpha())
Enter fullscreen mode Exit fullscreen mode

Output

True
False
False
False
Enter fullscreen mode Exit fullscreen mode

Example 2 – A practical example of the isalpha() method to check if the entered username is a valid alphabet or not.

username = input("Choose a username:")

if username.isalnum() == True:
    print("The entered username is ", username)
else:
    print("Please enter a valid usernameSrin.")
Enter fullscreen mode Exit fullscreen mode

Output

Choose a username:ItsMycode
The entered username is ItsMycode
Enter fullscreen mode Exit fullscreen mode

The post Python String isalpha() appeared first on ItsMyCode.

Top comments (0)