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
Write a program that does the following :
- takes two inputs : the first, an integer and the second, a string
- from the input string extract all the digits, in the order they occurred, from the string.
- if no digits occur, set the extracted digits to 0
- add the integer input and the digits extracted from the string together as integers
- print a string of the form :
"integerinput + stringdigits = sum"
For example :
For inputs 12, 'abc123' → '12 + 123 = 135'
For inputs 20, 'a5b6c7' → '20 + 567 =587'
For inputs 100, 'hi mom' → '100 + 0 = 100'Write a program that takes two strings from the user and displays the smaller string in single line and the larger string as per this format :
1st letter last letter 2nd letter 2nd last letter 3rd letter 3rd last letter
For example,
if the two strings entered are Python and PANDA then the output of the program should be :PANDA P n y o t h
Write a program that should do the following :
- prompt the user for a string
- extract all the digits from the string
- If there are digits:
- sum the collected digits together
- print out the original string, the digits, the sum of the digits
- If there are no digits:
- print the original string and a message "has no digits"
Sample
- given the input : abc123
prints abc123 has the digits 123 which sum to 6 - given the input : abcd
prints abcd has no digits
Write a program that should prompt the user to type some sentence(s) followed by "enter". It should then print the original sentence(s) and the following statistics relating to the sentence(s) :
- Number of words
- Number of characters (including white-space and punctuation)
- Percentage of characters that are alphanumeric
Hints
- Assume any consecutive sequence of non-blank characters is a word.