Python String isalpha()

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())

Output

True
False
False
False

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.")

Output

Choose a username:ItsMycode
The entered username is ItsMycode
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 String Find()

Python String find()

Table of Contents Hide find() Syntaxfind() Parametersfind() Return ValueDifference between find() method and index() methodExample 1: find() without any argumentsExample 2: find() with start and end Arguments The Python String…
View Post
Python Divmod()

Python divmod()

Table of Contents Hide divmod() Syntax divmod() Parametersdivmod() Return ValueExample: How divmod() works in Python?Errors And Exceptions  The divmod() in Python is a built-in function that takes two numbers as input…
View Post