Professional Documents
Culture Documents
Q 2. Write a program which replaces all vowels in the string with '*'.
SOLUTION
str = input("Enter a String : ")
vowel = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
new = "" # Empty string
for i in str :
if i in vowel :
new += "*"
else :
new += i
print("New String :", new)
OUTPUT
Enter a String : Programming
New String : Pr*gr*mm*ng
while True :
str = input("Please enter a sentence(enter 'q' to quit) :
")
newStr = ""
if str.lower() == "q" :
break
else:
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(enter 'q' to quit) : You wanna say
something?
yOU WANNA SAY SOMETHING?
Please enter a sentence(enter 'q' to quit) : No, I Don't
wanna
nO,i dON'T WANNA
Please enter a sentence(enter 'q' to quit) : q
Q 11. Write a program that asks the user for a string (only single space
between words) and returns an estimate of how many words are in the
string. (Hint. Count number of spaces)
SOLUTION
str = input("Enter a String : ")
count = 0
for ch in str :
if ch.isspace() :
count += 1
print("No. of Word :", (count+1))
OUTPUT
Enter a String : just because you're correct doesn't mean
you're right
No. of Word : 8
Q 12. Write a program to input a formula with some brackets and checks,
and prints out if the formula has the same number of opening and closing
parentheses.
SOLUTION
str = input("Enter a formula : ")
count = 0
for ch in str :
if ch == '(' :
count += 1
elif ch == ')' :
count -= 1
if count == 0 :
print("Formula has same number of opening and closing
parentheses.")
else :
print("Formula has unequal number of opening and closing
parentheses.")
OUTPUT
Enter a formula : (n/2)(2a + (n - 1)d)
Formula has same number of opening and closing parentheses.
Q 13. Write a program that inputs a line of text and prints out the count of
vowels in it.
SOLUTION
str = input("Enter a String : ")
vowel = ["a", "e", "i", "o", "u", "A", "E", "I", "O", "U"]
count = 0
for ch in str :
if ch in vowel :
count += 1
print("No. of vowels :", count)
OUTPUT
Enter a String : just because you’re correct doesn’t mean
you’re right
No. of vowels : 18
Q 14. 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()
longest = ''
for w in words :
if len(w) > len(longest) :
longest = w
print("Longest Word :", longest)
OUTPUT
Enter a string: I ran out of Ideas
Longest Word : Ideas
Q 15. Write a program to input a line of text and create a new line of text
where each word of input line is reversed.
SOLUTION
str = input("Enter a string : ")
words = str.split()
newStr = ""
for w in words :
rw = ""
for ch in w :
rw = ch + rw
newStr += rw + " "
print(newStr)
OUTPUT
Enter a string : really nothing
yllaer gnihton
— By Nitin Bhatt