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
Python3
# 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 whether string contains only number if ini_str.isalpha(): print ( "String contains only letters" ) else : print ( "String doesn't contains only letters" ) |
Initial String ababababa String contains only letters
Time complexity: O(n), where n is length of ini_str.
Auxiliary Space: O(1)
Method #2: Using re
Python3
# 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 whether 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" ) |
Initial String ababababa Contains only letters
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Without any builtin methods
Python3
# Python code to demonstrate # to find whether string contains # only letters # Initialising string ini_str = "ababababa" lowerletters = "abcdefghijklmnopqrstuvwxyz" upperletters = "ABCDEFGIJKLMNOPQRSTUVWXYZ" a = lowerletters + upperletters # Printing initial string print ( "Initial String" , ini_str) c = 0 for i in ini_str: if i in a: c + = 1 if (c = = len (ini_str)): print ( "String contains only letters" ) else : print ( "String doesn't contains only letters" ) |
Initial String ababababa String contains only letters
Method #4: Using operator.countOf() method
Python3
import operator as op # Python code to demonstrate # to find whether string contains # only letters # Initialising string ini_str = "ababababa" lowerletters = "abcdefghijklmnopqrstuvwxyz" upperletters = "ABCDEFGIJKLMNOPQRSTUVWXYZ" a = lowerletters + upperletters # Printing initial string print ( "Initial String" , ini_str) c = 0 for i in ini_str: if op.countOf(a, i): c + = 1 if (c = = len (ini_str)): print ( "String contains only letters" ) else : print ( "String doesn't contains only letters" ) |
Initial String ababababa String contains only letters
Time Complexity: O(N)
Auxiliary Space : O(1)
Method#5: Using Ord() method.
Approach: For each character, we use the ord() method to get the ASCII value of the character. Then we check if the ASCII value is within the range of ASCII values for uppercase and lowercase letters (65-90 and 97-122 respectively). If the ASCII value is not within these ranges, it means that the character is not a letter, and the code prints “String doesn’t contains only letters” and breaks out of the loop. If the loop completes without breaking, it means all the characters in the string are letters, and the code prints “String contains only letters”.
Python3
# Using ord() method to find whether string contains only letters # Initialising string ini_str = "ababababa" # Printing initial string print ( "Initial String" , ini_str) # Iterating through each character in the string for char in ini_str: # Using ord() method to get the ASCII value of the character ascii_val = ord (char) # If the ASCII value is not between 65 and 90 or 97 and 122 (ASCII values of uppercase and lowercase letters) if not (ascii_val > = 65 and ascii_val < = 90 ) and not (ascii_val > = 97 and ascii_val < = 122 ): print ( "String doesn't contains only letters" ) break else : print ( "String contains only letters" ) #this code contributed by tvsk |
Initial String ababababa String contains only letters
Time Complexity: O(N)
Auxiliary Space : O(1)