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

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

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

16