Python: Check if a given string contains only lowercase or uppercase characters
Python Basic - 1: Exercise-122 with Solution
Write a Python program to check if a given string contains only lowercase or uppercase characters.
Sample Solution-1:
Python Code:
# Define a function 'test' that checks if a string is entirely lowercase or uppercase.
def test(input_str):
# Use the 'islower()' and 'isupper()' methods to check if the string is entirely lowercase or uppercase.
return input_str.islower() or input_str.isupper()
# Test the 'test' function with different input strings.
str1 = "PHP"
print("Original string: ", str1)
print("Coded string: ", test(str1))
str2 = "javascript"
print("\nOriginal string: ", str2)
print("Coded string: ", test(str2))
str3 = "JavaScript"
print("\nOriginal string: ", str3)
print("Coded string: ", test(str3))
Sample Output:
Original string: PHP Coded string: True Original string: javascript Coded string: True Original string: JavaScript Coded string: False
Explanation:
Here is a breakdown of the above Python code:
- Test Function (test function):
- The "test()" function is defined to check if a given string is entirely in lowercase or uppercase.
- It uses the islower() and isupper() methods, which return 'True' if all characters in the string are lowercase or uppercase, respectively.
Flowchart:
Sample Solution-2:
Python Code:
# Define a function 'test' that checks if a string is entirely uppercase or lowercase.
def test(input_str):
# Check if the string is entirely in uppercase or lowercase.
return (input_str.upper() == input_str) or (input_str.lower() == input_str)
# Test the 'test' function with different input strings.
str1 = "PHP"
print("Original string: ", str1)
print("Coded string: ", test(str1))
str2 = "javascript"
print("\nOriginal string: ", str2)
print("Coded string: ", test(str2))
str3 = "JavaScript"
print("\nOriginal string: ", str3)
print("Coded string: ", test(str3))
Sample Output:
Original string: PHP Coded string: True Original string: javascript Coded string: True Original string: JavaScript Coded string: False
Explanation:
Here is a breakdown of the above Python code:
- Test Function (test function):
- The "test()" function is defined to check if a given string is entirely in uppercase or lowercase.
- It uses the upper() and lower() methods to create uppercase and lowercase versions of the string, respectively.
- The function returns 'True' if the original string is equal to either its uppercase or lowercase version.
Flowchart:
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to create a coded string from a given string, using specified formula.
Next: Write a Python program to remove the first and last elements from a given 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/basic/python-basic-1-exercise-122.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics