Python: Check whether lowercase letters exist in a string
Python Basic: Exercise-128 with Solution
Write a Python program to check whether lowercase letters exist in a string.
Pictorial Presentation:
Sample Solution-1:
Python Code:
# Assign the string 'A8238i823acdeOUEI' to the variable 'str1'.
str1 = 'A8238i823acdeOUEI'
# Check if there is any lowercase character in the string 'str1'.
# The 'any' function returns True if at least one character is lowercase.
print(any(c.islower() for c in str1))
Sample Output:
True
Sample Solution-2:
Python Code:
# Define a function 'lower_case_str' that takes a text as input.
def lower_case_str(text):
# Initialize a counter variable 'ctr' to keep track of lowercase characters.
ctr = 0
# Iterate through each character in the 'text'.
for char in text:
# Check if the character's Unicode value falls within the lowercase letters range.
if (ord(char) >= 97 and ord(char) <= 122):
ctr = ctr + 1
# If 'ctr' is greater than 0, it means at least one lowercase character was found.
if (ctr > 0):
return True
# Define a string 'str1' with mixed characters.
str1 = 'A8238i823acdeOUEI'
print("Original string:", str1)
# Call the 'lower_case_str' function to check if lowercase letters exist in 'str1'.
print("Lowercase letters exist in the said string: ", lower_case_str(str1))
# Repeat the above steps for different strings.
str1 = 'PYTHON'
print("\nOriginal string:", str1)
print("Lowercase letters exist in the said string: ", lower_case_str(str1))
str1 = 'javascript'
print("\nOriginal string:", str1)
print("Lowercase letters exist in the said string: ", lower_case_str(str1))
Sample Output:
Original string: A8238i823acdeOUEI Lowercase letters exist in the said string: True Original string: PYTHON Lowercase letters exist in the said string: None Original string: javascript Lowercase letters exist in the said string: True
Flowchart:
Check if all input alphabets are small:
Python Code:
# Define a function 'lower_case_str' that takes a text as input.
def lower_case_str(text):
# Check if the entire text is in lowercase using 'islower()' method.
if text.islower():
return True
return False
# Print a message to indicate the purpose of the code.
print("Check if all input alphabets are small!")
# Define a string 'str1' with mixed characters.
str1 = 'A8238i823acdeOUEI'
print("\nOriginal string:", str1)
# Call the 'lower_case_str' function to check if all characters in 'str1' are lowercase.
print("Lowercase letters exist in the said string: ", lower_case_str(str1))
# Repeat the above steps for different strings.
str1 = 'PYTHON'
print("\nOriginal string:", str1)
print("Lowercase letters exist in the said string: ", lower_case_str(str1))
str1 = 'javascript'
print("\nOriginal string:", str1)
print("Lowercase letters exist in the said string: ", lower_case_str(str1))
Sample Output:
Check if all input alphabets are small! Original string: A8238i823acdeOUEI Lowercase letters exist in the said string: False Original string: PYTHON Lowercase letters exist in the said string: False Original string: javascript Lowercase letters exist in the said string: True
Flowchart:
Python Code Editor:
Previous: Write a Python program to check whether an integer fits in 64 bits.
Next: Write a Python program to add leading zeroes to a string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/python-basic-exercise-128.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics