How to check if a character in a string is a letter in Python

In this article, you’ll learn how to check if a character in a string is a letter in python. Here, the letter denotes the alphabet and excludes all other numeric and special characters.

When you’re working with strings, you often have to face a situation where you need to check whether all the characters in a string are letters or not. Suppose you are writing a program that asks the user’s name and then stores his/her details. Firstly, you need to verify the user’s name by checking all the characters entered by the user. For this, check whether all the characters are letters or not.

If you want to learn more about strings and lists in Python, visit Python Tutorials.

using isalpha() method to determine if character is a letter

Python has a built-in Isalpha() function which returns true if the character is a letter otherwise returns false. Using for loop, traverse over the string and apply isalpha function on all the characters. This function will identify all numeric or special characters in a string. For better understanding, consider the following example.

#take a string as an input from user

input_str=str(input("Enter your name: "))

#iterate over the input string using for loop

for ch in input_str:
 
  #return true if the character is alphabet otherwise return False

  res=ch.isalpha()
  
  print(ch,res )
 

Output:

Enter your name: jo$n12
j True
o True
$ False
n True
1 False
2 False

You can directly apply Isalpha function over the string to check if the string contains only alphabets or not. For example

str1='Ali'

str2='[email protected]'

str3= 'David22'

print(str1.isalpha())

print(str2.isalpha())

print(str3.isalpha())
 

Output:

True
False
False

You can also check any specific character by its index. Suppose you want to check the fourth character in “str2”. You can do this by running the “str2[3].isalpha()” command. The fourth character in str2 is “!” which is not an alphabet therefore the program will print “False” on the output window. The Isalpha() function returns false if found space in a string. Therefore, this method won’t work when you have to verify the characters in a long string consisting of spaces or a sentence.

USING ISALPHA() AND ISSPACE() to check if a character in a string is letter

In the case of strings consisting of spaces, we can use isspace() function. It returns a true if space is detected otherwise returns false. By a combination of isalpha() and isspace() function, we can check whether all the characters in a string are alphabets and spaces or not.

def check_string(input_str):

  if all(x.isalpha() or x.isspace() for x in input_str):

    return True

  else:

    return False

if __name__ == "__main__" :

  string = input("Enter a string: ")

  print(check_string(string))
 

Output:

Enter a string: Hello World
True

In this article, you have learned how to identify if characters in a string is a letter or not in Python Programming. You have also learned how to deal with strings of sentences consisting of spaces. If you have any queries regarding this article, please let us know in the comments section. Contact us.

Leave a Comment

Your email address will not be published. Required fields are marked *