Python | Ways to check if given string contains only letter
Given a string, write a Python program to find whether a string contains only letters and no other keywords. Let’s discuss a few methods to complete the task.
Method #1: Using isalpha() method
# Python code to demonstrate # to find whether string contains # only letters # Initialising string ini_str = "ababababa" # Printing initial string print ("Initial String", ini_str) # Code to check whther string contains only number if ini_str.isalpha(): print("String contains only letters") else: print("String doesn't contains only letters") |
chevron_right
filter_none
Method #2: Using re
# Python code to demonstrate # to find whether string contains # only letters import re # Initialising string ini_str = "ababababa" # Printing initial string print ("Initial String", ini_str) # Code to check whther string contains only number pattern = re.compile("^[a-zA-Z]+$") if pattern.match(ini_str): print ("Contains only letters") else: print ("Doesn't contains only letters") |
chevron_right
filter_none
Recommended Posts:
- Python | Ways to check string contain all same characters
- Python | Ways to split a string in different ways
- Reverse string in Python (5 different ways)
- Python | Ways to check if element exists in list
- Python | Ways to concatenate boolean to string
- Ways to remove i'th character from string in Python
- Find length of a string in python (4 ways)
- Python | Ways to find all permutation of a string
- Python | Ways to convert list of ASCII value to string
- Python | Ways to sort letters of string alphabetically
- Python | Ways to count number of substring in string
- Python | Ways to convert string to json object
- Python | Ways to remove numeric digits from given string
- Python | Ways to find nth occurrence of substring in a string
- Python | Ways to remove n characters from start of given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



