0% found this document useful (0 votes)
131 views10 pages

String Manipulation - Output Questions

The document contains various string manipulation problems and their solutions, including code fragments that need correction, expected outputs for given inputs, and conceptual questions regarding string operations in Python. It also discusses the legality of certain operations with strings, the immutability of strings, and provides examples of string functions. Additionally, it addresses common errors and misconceptions related to string manipulation in Python.

Uploaded by

salirnaqvi2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views10 pages

String Manipulation - Output Questions

The document contains various string manipulation problems and their solutions, including code fragments that need correction, expected outputs for given inputs, and conceptual questions regarding string operations in Python. It also discusses the legality of certain operations with strings, the immutability of strings, and provides examples of string functions. Additionally, it addresses common errors and misconceptions related to string manipulation in Python.

Uploaded by

salirnaqvi2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

STRING MANIPULATION

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

Type A : Short Answer Questions / Conceptual Questions

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

Ans) a)’CARPE’ b)’DIEM’ c)’’ d)’ARPE’ e)’DIE’


7 From the string S = “CARPE DIEM”, which ranges return “DIE” and “CAR” ?
Ans)S[6:9]
S[0:3]
8 What would following expression return?
a)”Hello World”.upper( ).lower( ) (Ans)”hello world”
b) ”Hello World”.lower( ).upper( )(Ans)”HELLO WORLD”
c) ”Hello World”.find(“Wor”, 1 ,6)(Ans) -1
d) ”Hello World”.find(“Wor”) (Ans) 6
e) ”Hello World”.find(“wor”)(Ans) -1
f) ”Hello World”.isalpha( ) (Ans) False
g) ”Hello World”.isalnum( )(Ans) False
h)”1234”.isdigit( ) (Ans)True
i)”123FGH”.isdigit( )(Ans) False
0 1 2 3 4 5 6 7 8 9 10
H e l l o W o r l d
9 Which functions would you choose to use to remove leading and trailing white spaces
from a given string?
Ans)strip( )
10 Try to find out if for any case, the string function isalnum( ) and isalpha( ) return the
same result.
A They would atleast return the same result for all those cases where isalpha( ) is True.
Eg:
“amen’.isalpha( ) will be same as ‘amen’.isalnum( )
‘a#1’.isalpha( ) and ‘a#1’.isalnum( ) will both be False
11 Suggest appropriate functions for the following tasks:
i)To check whether the string contains digits (Ans) isdigit( )
ii)To find for the occurrence a string within another string (Ans) find( )
iii)To convert the first letter of a string to upper case. (Ans) capitalize( )
iv)to capitalize all the letters of the string (Ans) upper( )
v)To check whether all letters of the string are in capital letters(Ans)isupper( )
vi)To remove from right of a string all string – combinations from a given set of
letters.(Ans) rstrip([chars])
vii)To remove all white spaces from the beginning of a string.(Ans)lstrip( )

Type b : Application Based Questions

1 What is the result of the following expressions?


d)
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
o/p

e)
0 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1
o/p

2 What will be the output produced by following code fragments?

#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.

You might also like