Python program to check if lowercase letters exist in a string
Last Updated :
03 Apr, 2023
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
str = "Live life to the Fullest"
for char in str :
k = char.islower()
if k = = True :
print ( 'True' )
break
if (k ! = 1 ):
print ( 'False' )
|
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
str = "GEEKS"
for char in str :
k = char.isupper()
if k = = False :
print ( 'False' )
break
if (k = = 1 ):
print ( '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
x = "GEeK"
counter = 0
for char in x:
if ( ord (char) > = 65 and ord (char) < = 90 ):
counter = counter + 1
if ( ord (char) > = 97 and ord (char) < = 122 ):
print ( "Lower Character found" )
break
if counter = = len (x):
print ( True )
|
Output
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
import re
str1 = "Live life to the Fullest"
k = re.search( '[a-z]' , str1)
if (k is None ):
print ( 'False' )
else :
print ( 'True' )
|
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Without using any builtin methods
Python3
str = "Live life to the Fullest"
loweralpha = "abcdefghijklmnopqrstuvwxyz"
res = False
for char in str :
if char in loweralpha:
res = True
break
print (res)
|
Method #6 : Using replace() and len() methods
Python3
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)
|
Method 7: Using operator.countOf() method
Python3
import operator as op
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)
|
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
str = "Live life to the Fullest"
result = any ( ord (char) > = 97 and ord (char) < = 122 for char in str )
print (result)
|
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)
|
Time Complexity – O(n) # n is the length of the string.
Auxiliary Space – O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...