In this tutorial, we will look at how to check if a string in Python starts with a letter (alphabet) or not with the help of some examples.
How to check if a string starts with an alphabet character?

To check if a string in Python starts with a letter or not, check if the first character in the string is an alphabet or not using the string isalpha()
function.
The built-in string isalpha()
function in Python takes a string and returns True
if all the characters in the string are alphabet and there is at least one character in the string. It returns False
otherwise.
So, use the string isalpha()
function to check if the first character in the string (character at index 0
) is an alphabet or not. The following is the syntax –
# check if string starts with an alphabet s[0].isalpha()
It returns True
if the string starts with an alphabet.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
Examples
Let’s now look at some examples. First, we will create some strings that we will use throughout this tutorial.
# create strings s1 = "apple" s2 = "7eleven" s3 = "$500" s4 = "" # display the strings print(s1) print(s2) print(s3) print(s4)
Output:
apple 7eleven $500
Here, we created four strings – s1
, s2
, s3
and s4
. The string s1
starts with a letter, the string s2
starts with a number, the string s3
starts with the special character '$'
and the string s4
is an empty string.
Let’s now check if the strings s1
, s2
, and s3
start with an alphabet or not.
# check if string starts wtih a letter print(s1[0].isalpha()) print(s2[0].isalpha()) print(s3[0].isalpha())
Output:
True False False
We get True
for s1
as it does start with an alphabet. We get False
for s2
and s3
as they do not start with an alphabet.
Now let’s apply the above method to the empty string, s4
.
# check if string starts wtih a letter print(s4[0].isalpha())
Output:
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) Input In [4], in <module> 1 # check if string starts wtih a letter ----> 2 print(s4[0].isalpha()) IndexError: string index out of range
We get an IndexError
because we’re trying to access an index that does not exist (an empty string does not have a 0
index).
To avoid the above error, you can first check whether the string is non-empty and then proceed to check if the first character is an alphabet or not. See the example below –
# function to check if string starts wtih a letter def check_str_starts_with_alpha(s): if s and s[0].isalpha(): return True else: return False # check if string starts wtih a letter print(check_str_starts_with_alpha(s4))
Output:
False
We get False
as the output for the empty string.
You might also be interested in –
- Python – Check If a Character Appears Twice in String
- Python – Check If String Contains Vowels
- Python – Check If String Contains Lowercase Letters
- Python – Check If String Contains Uppercase Letters
- Python String Uppercase – With Examples
- Python String Lowercase – With Examples
- Python – Capitalize First Letter of Each Word
- Python – Check If All Characters in String are Same
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.