Ways to Check if a given String in Python Contains Only Letters


Python is used by programmers all over the world for different purposes such as web development, data science, machine learning and to perform various different processes with automation. In this article we are going to learn about different ways to check if a given string in python contains only characters.

Different Methods to check if a given string contains only letters

Isalpha Function

This is the simplest method of checking if a given string in python contains letters. It will give the output as true and false as per the presence of letters in the string. Let’s take an example to understand it in a better way:

Example

def letters_in_string(string): # A new function is created giving the string as input and the function isalpha is run in it to check the presence of letters 
    return string.isalpha() 

# Example 
main_string = "Hi! I am John." # The string is given as input
check = letters_in_string(main_string) # The function letter_in_string is run
print(check)  # The output will be displayed as true or false

Output

The output of the above example will be as follows:

False 

Regular Expression

The regular expression module is used to work with the regular expression present in the python programme. This is a very simple method to check if a string contains only letters. Lets take an example to understand it in a better way:

Example

import re # Do not forget to import re or else error might occur

def letters_in_string(string): # The function is given with input of string
    pattern = r'^[a-zA-Z]+$'  # All the different alphabetic characters will be detected
    return re.match(pattern, string) is not None # The match function of the regular expression module will be given the string as input and it will check if only letters are present in the string

# Example 
main_string = "MynameisJohn" # The string is given as input
check = letters_in_string(main_string) # The string is given as input
print(check)

Output

The output of the above example will be as follows:

True 

ASCII Values

This is a complex method but it is a very efficient method of finding if a string contains only letters in it. In ASCII different codes are given to different characters. So, in this method we will check if the string contains the characters within the defined range. Lets take an example to understand it in a better way:

Example

def letters_in_string(string): # A function is defined with the string as input
    for char in string:
        ascii_val = ord(char) # The ASCII value will be found for different characters in the input
        if not (65 <= ascii_val <= 90 or 97 <= ascii_val <= 122): # A range is defined and if the characters will be within defined range then the output will be as true and if the characters are not within the range it will be displayed as output
            return False
    return True

# Example 
main_string = "MynameisJohn"
check = letters_in_string(main_string)
print(check)

Output

The output of the above code will be as follows:

True

For Unicode Characters

This is a very special case in which if the string is given the input of Unicode characters, then there are chances of displaying the wrong output. So, in such a case we will use the regular expression module with Unicode characters. Let’s take an example to understand it in a better way:

Example

import unicodedata # Do not forget import unicodedata or else error might occur

def letters_in_strings(string): # A new function is run with string as the input
    for char in string:
        if not unicodedata.category(char).startswith('L'):
            return False
    return True

# Example 
input_string = "こんにちは"
result = letters_in_strings(input_string)
print(result) 

Output

The output of the above example will be as follows:

True 

Conclusion

There are numerous ways to determine in Python if a given string solely includes letters. The best course of action depends on your unique requirements. The isalpha() function, regular expressions with ASCII values, regular expressions with Unicode character characteristics, and iterating over the characters in the string were the four methods covered in this article. Using these methods, you may quickly determine in your Python programmes whether a string contains just alphabetic letters.

Updated on: 01-Aug-2023

127 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements