String Manipulation - Output Questions
String Manipulation - Output Questions
Solved Problems
2 Figure out the problem with following code fragment. Correct the code and
then print the output.
1. s1 =’must ’
2. s2 =’try ’
3. n1 =10
4. n2 = 3
5. print(s1 + s2)
6. print(s2 * n2)
7. print(s1 + n1)
8. print(s2 *s1)
Ans)
must try
try try try
must 10
try must
3 Consider the following code:
string= input(“Enter a string :”)
count = 3
while True:
if string[0] == ‘a’:
string = string[2: ]
elif string[-1] == ‘b’:
string = string[:2]
else:
count += 1
break
print(string)
print(count)
What will be the output produced, if the input is
i)aabbcc ii)aaccbb iii)abcc
Ans)
a)bbcc b)cc c)cc
4 4 4
4 Consider the following code:
Inp = input(“Please enter a string:”)
while len(Inp) <= 4 :
if Inp[-1] == ‘z’:
Inp = Inp[0:3] + ‘c’
elif ‘a’ in Inp:
Inp = Inp[0] + ‘bb’
elif not int(Inp[0]):
Inp = ‘1’ + Inp[1:] +’z’
else:
Inp = Inp + ‘*’
print(Inp)
What will be the output produced if the input is
i)1bzz ii)’1a’ iii)’abc’ iv)’0xy’ v)’xyz’
Ans
i)1bzc* ii)1bb** iii)endless loop iv)1xyc*
v)Raises an error as Inp[0] cannot be converted to int
2 Out of the following operators , which ones can be used with strings?
= , - , * , / , // , % , > , < > , in , not in , <=
Ans)= ,* , > , in , notin ,<=
3 What is the result of following statement, if the input is ‘Fun’?
print( input(“……”) + ”trial” + ”Ooty” *3)
Ans)FuntrialOotyOotyOoty
4 Which of the following is not a python legal string operation?
a) ‘abc’ +’abc’ b) ‘abc’ * 3
c)’abc’ + 3 d)’abc’.lower( )
Ans)c
5 Can you say strings are character lists? Why? Why not?
A Although strings are sequences of characters they are immutable and hence are not
truly a character list.For eg:
a =’ban’
a[1] =’c’ is invalid
where as
a=[‘b’, ’a’, ’n’]
a[1] = ‘c’ is valid
6 Given a string S = “CARPE DIEM”. If n is length/2 ( length is the length of the given
string), then what would following return?
a) S[ : n] b)S[n: ] c)S[n : n]
d) S[1 : n] e)S[n : length-1]
0 1 2 3 4 5 6 7 8 9
C A R P E D I E M
e)
0 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1
o/p
#because statement has been extended to the next line , the correct way is
# x = “hello” + “to Python” + “world”
o/p
c)
0 1 2 3 4 5 6 7 8 9 10
h e l l o w o r l d
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
o/p
3 Carefully go through the code given below and answer the questions based on it:
Ans
i)a ii)d iii)e iv)b
4 Carefully go through the code given below and answer the questions based on it:
testStr = “abcdefghi”
Ans
i)c ii)d iii)e iv)d v)e
5 Carefully go through the code given below and answer the questions based on it:
Ans
i)a ii)c iii)d iv)d v)b
6 Carefully go through the code given below and answer the questions based on it:
in1Str = input(“Enter string of digits :”)
in2Str = input(“Enter string of digits :”)
if len(in1Str) > len(in2Str)
small = in2Str
large = in1Str
else:
small = in1Str
large = in2Str
newStr = ‘ ‘
for element in small:
result = int(element) + int(large[0])
newStr = newStr + str(result)
large = large[1 :]
print (len(newStr)) #Line1
print (newStr) #Line2
print(large) #Line3
print(small) #Line4
1)Given a first input of 12345 and a second input of 246 , what result is produced by
Line1?
a) 1 b) 3 c) 5 d) 0 e)None of these
2)Given a first input of 12345 and a second input of 246 , what result is produced by
Line2?
a) 369 b)246 c) 234 d) 345 e)None of these
3) Given a first input of 123 and a second input of 4567 , what result is produced by
Line3?
a) 3 b) 7 c)12 d)45 e)None of these
4) Given a first input of 123 and a second input of 4567 , what result is produced by
Line4?
a)123 b)4567 c) 7 d) 3 e) None of these
Ans:
1)b 2)a 3)b 4)a
7 Find the output if the input is Test
a)
S=input(“Enter String :”)
RS =” “
for ch in S:
RS = ch + RS
print(S + RS)
o/p
TesttseT
b)
S=input(“Enter String :”)
RS =” “
for ch in S:
RS = ch + 2 + RS # Error concatenation with string and number not possible
print(RS + S)
8 Find the errors
a) b)
S=”PURA VIDA” S=”PURA VIDA”
print( S[9] + S[9 : 15] ) S1= S[ : 10] + S[10 : ]
Ans)String index out of range S2 = S[10] + S[-10]
Ans)String index out of range
c) d)
S=”PURA VIDA” S=”PURA VIDA”
S1 = S * 2 S1 = S[ : 5]
S2 = S1[ -19] + S1[-20] S2 = S[5 : ]
S3 = S1[-19 : ] S3 = S1 * S2
Ans)String index out of range S4 = S2 + ‘3’
S5 = S1 + 3
Ans)(*)Replication operator cannot work
with both operands of string types .
(+) operator cannot work with one
operand as string and one as a number.