w3resource

Python: Check if a given string contains only lowercase or uppercase characters


Check Lower or Uppercase

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:

Flowchart: Python - Check if a given string contains only lowercase or uppercase characters.

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:

Flowchart: Python - Check if a given string contains only lowercase or uppercase characters.

For more Practice: Solve these Related Problems:

  • Write a Python program to check if a given string contains only uppercase letters.
  • Write a Python program to verify if a string consists solely of lowercase characters.
  • Write a Python program to test whether a string is uniformly cased (either all uppercase or all lowercase).
  • Write a Python program to determine if a string satisfies the condition of being entirely in one case (upper or lower).

Go to:


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.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.