WORK SHEET : STRING
Question 1
Write a program which replaces all vowels in the string with '*'.
Solution
str = input("Enter the string: ")
newStr = ""
for ch in str :
lch = ch.lower()
if lch == 'a' or lch == 'e' or lch == 'i' or lch == 'o' or lch == 'u' :
newStr += '*'
else :
newStr += ch
print(newStr)
Output
Enter the string: Computer Studies
C*mp*t*r St*d**s
Question 2
Write a program which reverses a string and stores the reversed string in a new string.
Solution
str = input("Enter the string: ")
newStr = ""
for ch in str :
newStr = ch + newStr
print(newStr)
Output
Enter the string: computer studies
seiduts retupmoc
Question 3
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, print the string produced from, by converting each lower case letter to
upper case and each upper case letter to lower case.
All other characters are left will be 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
Solution
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
Question 4
Write a program to input a line of text and print the biggest word (length wise) from it.
Solution
str = input("Enter a string: ")
words = str.split()
longWord = ' '
for w in words :
if len(w) > len(longWord) :
longWord = w
print("Longest Word =", longWord)
Output
Enter a string: TATA FOOTBALL ACADEMY WILL PLAY AGAINST MOHAN BAGAN
Longest Word = FOOTBALL
Question 5
Write a program to input a string and display the word which has vowel ‘a’ lin that word.
Question 6
Write a program to input a string and replace all ‘a’ with the symbol ‘@’ .
Question 7
Write a program to input a string and find total number of digits .
Question 8
Write a program to input a string and find total number of vpwels.