In this Python tutorial, we will learn to check if a character in a string is a letter using Python programming language.
For this, we will have a string in which we will have some string and a integer. Then we will look for letters in that string.
Table Of Contents
Method 1 : Using isalpha() method
First method that we will be using is the isalpha() method of string class. It returns True if all charaters are alphabets and return False otherwise.
SYNTAX : string.isalpha()
Here, first we will check for whole string variable whether it has integers inside it or has only alphabets/letters. It will return True if the given string has letters only. If the given string has both characters/letters and integers then it will return False.
See the example code below,
# initialized a string with integer and letters str_var = '312341AFc' # returns true if string has only letters else returns false print(str_var.isalpha())
Now, to iterate and check for characters inside the string, we will be using for loop with isalpha() method to check if each character in the string is a letters or not.
See the Example code below
# initialized a string with integer and letters str_var = '312341AFc' # iterating over the characters of the string # to check if it has letters or not. for char in str_var: result = char.isalpha() print(char, ':', result)
3 : False 1 : False 2 : False 3 : False 4 : False 1 : False A : True F : True c : True
In the code and output above, we have used the isalpha() method to check if the character in a string is a letter or not. First we checked the whole string then we iterated over the string variable and checked for the each char, if it is letter or not. It prints true in front of character which determines that it is a letter.
Method 2 : Checking using ASCII values and ord() function
Now in this method, we will be using the ASCII values and ord() function to check if character in a string is a letter.
But first what is ASCII Value and ord() function?
ASCII Value :: ASCII Stands for American Standard Code for Information Interchange, and it is the most used character encoding format.It is a 7-bit character code in which every single bit represents a unique character.
Every character in english alphabet has a unique ASCII code.
- ASCII code of A to Z (Upper Case) starts from 065 and ends at 090.
- ASCII code of a to z (Lower Case) starts from 097 and ends at 122.
ord() function : The ord() function is a built in function in Python programming language, which takes a unicode character as an argument and returns the ASCII code of that character.
Here what we can do is, loop through all the characters in the given string, and using ord() fucntion check if the ASCII value of the each character lies between 65 to 90 or between 97 to 122.
See the example code below,
# initialized a string with integer and letters str_var = "13A24K3243253434s59w459" # iterating through str_var for x in str_var: # checking for upper case alphabets if ord(x) >= 65 and 90 >= ord(x): print('Found letter', x) # checking for lower case alphabets elif ord(x) >= 97 and 122 >= ord(x): print('Found letter', x)
Found letter A Found letter K Found letter s Found letter w
In the code and output above, using the ord() function we successfully found out the letters in the string str_var. In this method we fetched the ASCII value of each character in string using order() function and then checked if they are letters or not.
In this Python tutorial, we used two different methods to check if a character in a string is a letter using Python Programming Language. You can always use any of the methods depending upon your requirement but the most used and easy to understand and also has the shorter syntax is method 1 which is by using isalpha() , method 2 also can be very useful because through which you can find for special characters in the given string but you need to know about ASCII values.
Also we have used Python 3.10.1 for writing examples. Type python –version to check your python version. Always try to read, write & understand example codes. Happy Coding.
Thanks for reading.