String Manipultion Type C
String Manipultion Type C
Question 1
Solution
Output
Question 2
'*'.
Solution
Output
Question 3
Solution
Output
Question 4
digits and two dashes, with dashes after the area code and
Solution
Output
=====================================
Question 5
the digits
digits"
Sample
Solution
Output
Question 6
the sentence(s) :
• Number of words
punctuation)
Hints
characters is a word.
Solution
print("Original Sentences:")
print(str)
Output
Question 7
quit.
• Upon input of a sentence s, print the string produced
For example,
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
Question 8
a string
• from the input string extract all the digits, in the order
• add the integer input and the digits extracted from the
For example :
Solution
for ch in str :
if ch.isdigit() :
digitsStr += ch
if digitsStr :
digitsNum = int(digitsStr)
Output
Enter an integer: 12
Enter the string: abc123
12 + 123 = 135
=====================================
Enter an integer: 20
Enter the string: a5b6c7
20 + 567 = 587
=====================================
Question 9
Write a program that takes two strings from the user and
For example,
if the two strings entered are Python and PANDA then the
PANDA
P n
y o
t h
Solution
small = str1
large = str2
print(small)
lenLarge = len(large)
for i in range(lenLarge // 2) :
print(' ' * i, large[i], ' ' * (lenLarge - 2 * i),
large[lenLarge - i - 1], sep='')
Output
Question 10
left.
1 = 6.
the symbols are "I" and "V", and the coding works like
this. "I" , "II", "III", "IV", "V", "VI", "VII", "VIII", "IX".
• The same rules work for numbers from 10 to 90, using
"X" and "L". For numbers from 100 to 900, using the
3888= MMMDCCCLXXXVIII
Solution
result = ''
for i in range(len(num)) :
count = int(n / num[i])
result += str(rom[i] * count)
n -= num[i] * count
print(result)
Output
=====================================
=====================================
Enter the number: 3888
MMMDCCCLXXXVIII
11
Question 11
Write a program that asks the user for a string (only single
spaces)
Solution
Output
12
Question 12
Write a program to input a formula with some brackets and
checks, and prints out if the formula has the same number
Solution
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
=====================================
13
Question 13
Write a program that inputs a line of text and prints out the
Solution
for ch in str :
lch = ch.lower()
if lch == 'a' ¥
or lch == 'e' ¥
or lch == 'i' ¥
or lch == 'o' ¥
or lch == 'u' :
count += 1
Output
14
Question 14
Solution
Output
15
Question 15
Solution
for w in words :
rw = ""
for ch in w :
rw = ch + rw
newStr += rw + " "
print(newStr)
Output