KnowledgeBoat Logo

Computer Science

Write a Python program as per specifications given below:

  • Repeatedly prompt for a sentence (string) or for 'q' to quit.
  • Upon input of a sentence s, print the string produced from s by converting each lower case letter to upper case and each upper case letter to lower case.
  • All other characters are left unchanged.

For example,
Please enter a sentence, or 'q' to quit : This is the Bomb!
tHIS IS THE bOMB!
Please enter a sentence, or 'q ' to quit : What's up Doc ???
wHAT'S UP dOC ???
Please enter a sentence, or 'q' to quit : q

Python

Python String Manipulation

8 Likes

Answer

while True :
    str = input("Please enter a sentence, or 'q' to quit : ")
    newStr = ""
    if str.lower() == "q" :
        break
    for ch in str :
        if ch.islower() :
            newStr += ch.upper()
        elif ch.isupper() :
            newStr += ch.lower()
        else :
            newStr += ch
    print(newStr)

Output

Please enter a sentence, or 'q' to quit : This is the Bomb!
tHIS IS THE bOMB!
Please enter a sentence, or 'q' to quit : What's up Doc ???
wHAT'S UP dOC ???
Please enter a sentence, or 'q' to quit : q

Answered By

4 Likes


Related Questions