Contents
- Introduction
- Syntax of isalpha[]
- Example 1: Check if String contains only Alphabets
- Example 2: Check if String contains only Alphabets
- Summary
To check if a string contains only alphabets, use the function isalpha[] on the string. isalpha[] returns boolean value. The return value is True if the string contains only alphabets and False if not.
In this tutorial, we will write Python programs to check if the given string contains only alphabets [upper or lower].
Syntax of isalpha[]
The syntax of String.isalpha[] function is given below.
bool = str1.isalpha[]
Example 1: Check if String contains only Alphabets
Let us create a string and check if the string contains only alphabets.
Python Program
str1 = "hello world. welcome to python examples."
bool = str1.isalpha[]
print['str1 contains only alphabets:', bool]
Run
Output
str1 contains only alphabets: False
str1 contains two full stops .
and some spaces along with alphabets. So isalpha[] returned false.
Example 2: Check if String contains only Alphabets
Now, let us check with a string with only alphabets.
Python Program
str1 = "helloworldpythonexamples"
bool = str1.isalpha[]
print['str1 contains only alphabets:', bool]
Run
Output
str1 contains only alphabets: True
Summary
In this tutorial of Python Examples, we learned how to check if a string contains only Alphabets or not, with the help of well detailed Python example programs.
Related Tutorials
- How to Check if given String contains only Alphanumeric Characters in Python?
- Python – Check if String contains Substring from List
- Python – Check if String contains Substring
In this tutorial, we will look at how to check whether a string contains only letters or not in Python with the help of examples.
How to check if a string contains only alphabets in Python?
You can use the string isalpha[]
function to check if a string contains only letters [that is,
alphabets] in Python. The following is the syntax –
# check if string s contains only letters s.isalpha[]
It returns True
if all the characters in the string are alphabets. If any of the characters is not an alphabet, it returns False
.
Let’s look at an example.
# create a string s = "Bucky" # check if string contains only alphabets print[s.isalpha[]]
Output:
True
We get True
as the output. This is because all the characters in the string s
above are alphabets.
Let’s look at another example.
# create a string s = "Bucky7" # check if string contains only alphabets print[s.isalpha[]]
Output:
False
Here, we get False
as the output. This is
because not all characters in the string s
above are alphabets. One of the characters in the string, “7” is a digit.
Also, be careful if you’re string contains any punctuations. For example, for the string “you’re” the isalpha[]
function returns False
.
# string with punctuations s = "you're" # check if string contains only alphabets print[s.isalpha[]]
Output:
False
This is because not all characters in the string are alphabets. The character '
is not an alphabet.
Again, this is expected because the objective of the isalpha[] function is to check if all the characters are alphabets or not.
For more on the string isalpha[] function, refer to its documentation.
You might also be interested in –
- Python – Check if String Contains Only Numbers
- Python – Remove Non Alphanumeric Characters from String
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Piyush is a data scientist passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.
View all posts