w3resource

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:

def test(str):
    return str.islower() or str.isupper()

str = "PHP"
print("Original string: ",str)
print("Coded string: ",test(str))
str = "javascript"
print("\nOriginal string: ",str)
print("Coded string: ",test(str))
str = "JavaScript"
print("\nOriginal string: ",str)
print("Coded string: ",test(str))

Sample Output:

Original string:  PHP
Coded string:  True

Original string:  javascript
Coded string:  True

Original string:  JavaScript
Coded string:  False

Flowchart:

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

Sample Solution-2:

Python Code:

def test(str):
    return (str.upper() == str) or (str.lower() == str)

str = "PHP"
print("Original string: ",str)
print("Coded string: ",test(str))
str = "javascript"
print("\nOriginal string: ",str)
print("Coded string: ",test(str))
str = "JavaScript"
print("\nOriginal string: ",str)
print("Coded string: ",test(str))

Sample Output:

Original string:  PHP
Coded string:  True

Original string:  javascript
Coded string:  True

Original string:  JavaScript
Coded string:  False

Flowchart:

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

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.



Follow us on Facebook and Twitter for latest update.