Python program to check if lowercase letters exist in a string
Given a string, the task is to write a Python program to check if the string has lowercase letters or not.
Examples:
Input: "Live life to the fullest" Output: true Input: "LIVE LIFE TO THe FULLEST" Output: true Input: "LIVE LIFE TO THE FULLEST" Output: false
Methods 1#: Using islower()
It Returns true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
Python
# Python code to demonstrate working of # is.lower() method in strings #Take any string str = "Live life to the Fullest" # Traversing Each character of # the string to check whether # it is in lowercase for char in str : k = char.islower() if k = = True : print ( 'True' ) # Break the Loop when you # get any lowercase character. break # Default condition if the string # does not have any lowercase character. if (k ! = 1 ): print ( 'False' ) |
True
Methods 2#: Using isupper()
It Returns true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.
Python
# Python Program to demonstrate the # is.upper() method in strings # Take a string str = "Beginner" # Traversing Each character # to check whether it is in uppercase for char in str : k = char.isupper() if k = = False : print ( 'False' ) # Break the Loop # when you get any # uppercase character. break # Default condition if the string # does not have any uppercase character. if (k = = 1 ): print ( 'True' ) |
True
Methods 3#: Using ASCII Value to check whether a given character is in uppercase or lowercase.
The ord() function returns an integer representing the Unicode character.
Example:
print(ord('A')) # 65 print(ord('a')) # 97
Python3
# Python Program to Demonstrate the strings # methods to check whether given Character # is in uppercase or lowercase with the help # of ASCII values #Input by Beginner x = "GEeK" # counter counter = 0 for char in x: if ( ord (char) > = 65 and ord (char) < = 90 ): counter = counter + 1 # Check for the condition # if the ASCII value is Between 97 and 122 # if condition is True print the # corresponding result if ( ord (char) > = 97 and ord (char) < = 122 ): print ( "Lower Character found" ) break if counter = = len (x): print ( True ) |
Lower Character found
Method#4: Using re.search()
re.search module is used to search for the pattern in string. We can use the lower case as a pattern to search in string.
Python3
# Python code to find the existence of # lower case in string # importing re module import re # Take any string str1 = "Live life to the Fullest" # Searching lower case char in string using re.search k = re.search( '[a-z]' , str1) # Default condition if search does not find # any lower case # else have lower case in string if (k is None ): print ( 'False' ) else : print ( 'True' ) |
True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Without using any builtin methods
Python3
# Python code to check whether lowercase characters exist in string # Take any string str = "Live life to the Fullest" loweralpha = "abcdefghijklmnopqrstuvwxyz" res = False for char in str : if char in loweralpha: res = True break print (res) |
True
Method #6 : Using replace() and len() methods
Python3
# Python code to check whether lowercase characters exist in string # Take any string str = "Live life to the Fullest" upperalpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" res = False for i in upperalpha: str = str .replace(i, "") if ( len ( str ) > = 1 ): res = True print (res) |
True
Method 7: Using operator.countOf() method
Python3
# Python code to check whether lowercase characters exist in string import operator as op # Take any string str = "Live life to the Fullest" loweralpha = "abcdefghijklmnopqrstuvwxyz" res = False for char in str : if op.countOf(loweralpha, char) > 0 : res = True break print (res) |
True
Time Complexity: O(N)
Auxiliary Space : O(1)
Method #8 : Using the any() and ord() functions
In this approach, we use the any() function and ord() function to check if lowercase letters exist in the given string. The any() function returns True if any element of the iterable is True and False otherwise. The ord() function returns an integer representing the Unicode code point of the given single character string.
We use a generator expression in the any() function that checks for each character in the string if its ASCII value is between 97 and 122 (the range for lowercase letters). If any character in the string has an ASCII value in this range, the any() function returns True.
Python3
#Python code to check whether lowercase characters exist in string #Take any string str = "Live life to the Fullest" #Using any() and ord() functions to check if lowercase letters exist result = any ( ord (char) > = 97 and ord (char) < = 122 for char in str ) print (result) |
True
Time complexity: O(n)
Auxiliary Space: O(1)
Method #9 – Using string module’s ascii_lowercase method
In this approach, we will use the String module’s ascii_lowercase method to check if lowercase character exists in a string or not.
Step – 1: We will import the module required i.e string.
Step – 2: we will use a string initialized with a value already.
Step – 3: Then we will use a boolean variable and initialize it to false.
Step 4: we will iterate over the ascii_lowercase method and check if any of the element present in ascii_lowercase is also present in our string. If found then we will update the value of that boolean variable into True and break from the loop.
Step – 5: We will print the value of that boolean variable.
Example:
Python3
import string str = "Live life to the Fullest" res = False for i in string.ascii_lowercase: if i in str : res = True break print (res) |
True
Time Complexity – O(n) # n is the length of the string.
Auxiliary Space – O(1)