Python - Check If String Contains Only Letters

Sovary May 28, 2022 366
1 minute read

In Python to check whether if a string contain only alphabets we use isalpha() function on a string. isalpha() function is built-in string function which will returns as boolean value.

This short tutorial we given example below how to check given string have only alphabet or not.

isalpha() - to check if character is an alphabet or not by return value as boolean (true if alphabet, else not)

Syntax

str.isalpha():boolean

Return Value: Returns boolean. True if str contain only alphabet, False if str contain at least one none alphabet.

Examples

Example #1

text = "cambotutorial is for free everyone."
check = text.isalpha()

print("Is contain only alphabets? ", check)

Output

Is contain only alphabets?  False

Return false because there is a full stop[.] and there are some space between each word.

Example #2

text = "cambotutorialForEveryone"
check = text.isalpha()

print("Is contain only alphabets? ", check)

Output

Is contain only alphabets?  True

Return true because no space and none other characters beside alphabet.

Example #3

text = "contact@cambotutorial.com for everyone"
check = text.isalpha()

print("Is contain only alphabets? ", check)

Output

Is contain only alphabets?  False

Return false because there are special characters @ . and space existing in stirng.

Conclusion

In this tutorial we simply learned how to know if a string contains only alphabets or not, within example above hope it will help to handle your task.

Python 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him