Python MCQs on String: SET 13

121. What will be the output of the following Python code snippet?

print(‘abcdefcdghcd’.split(‘cd’, 2))

a) [‘ab’, ‘ef’, ‘ghcd’]

b) [‘ab’, ‘efcdghcd’]

c) [‘abcdef’, ‘ghcd’]

d) none of the mentioned

Check Answer

Answer: a
The string is split into a maximum of maxsplit+1 substrings.

 

 

122. What will be the output of the following Python code snippet?

print(‘ab\ncd\nef’.splitlines())

a) [‘ab’, ‘cd’, ‘ef’]

b) [‘ab\n’, ‘cd\n’, ‘ef\n’]

c) [‘ab\n’, ‘cd\n’, ‘ef’]

d) [‘ab’, ‘cd’, ‘ef\n’]

Check Answer

Answer: a
It is similar to calling split(‘\n’).

 

 

123. What will be the output of the following Python code snippet?

print(‘Ab!2’.swapcase())

a) [email protected]

b) ab12

c) aB!2

d) [email protected]

Check Answer

Answer: c
Lowercase letters are converted to uppercase and vice-versa.

 

 

124. What will be the output of the following Python code snippet?

print(‘ab cd ef’.title())

a) Ab cd ef

b) Ab cd eF

c) Ab Cd Ef

d) None of the mentioned

Check Answer

 

Answer: c
The first letter of every word is capitalized.

 

125. What will be the output of the following Python code snippet?

print(‘ab cd-ef’.title())

a) Ab cd-ef

b) Ab Cd-ef

c) Ab Cd-Ef

d) None of the mentioned

Check Answer

Answer: c
The first letter of every word is capitalized. Special symbols terminate a word.

 

 

126. What will be the output of the following Python code snippet?

print(‘abcd’.translate(‘a’.maketrans(‘abc’, ‘bcd’)))

a) bcde

b) abcd

c) error

d) bcdd

Check Answer

Answer: d
The output is bcdd since no translation is provided for d.

 

 

127. What will be the output of the following Python code snippet?

print(‘abcd’.translate({97: 98, 98: 99, 99: 100}))

a) bcde

b) abcd

c) error

d) none of the mentioned

Check Answer

Answer: d
The output is bcdd since no translation is provided for d.

 

 

128. What will be the output of the following Python code snippet?

print(‘abcd’.translate({‘a’: ‘1’, ‘b’: ‘2’, ‘c’: ‘3’, ‘d’: ‘4’}))

a) abcd

b) 1234

c) error

d) none of the mentioned

Check Answer

Answer: a
The function translate expects a dictionary of integers. Use maketrans() instead of doing the above.

 

 

129. What will be the output of the following Python code snippet?

print(‘ab’.zfill(5))

a) 000ab

b) 00ab0

c) 0ab00

d) ab000

Check Answer
Answer: a
The string is padded with zeros on the left hand side. It is useful for formatting numbers.

 

 

130. What will be the output of the following Python code snippet?

print(‘+99’.zfill(5))

a) 00+99

b) 00099

c) +0099

d) +++99

Check Answer
Answer: c
zeros are filled in between the first sign and the rest of the string.

Pages  1  2  3  4  5  6  7  8  9  10  11  12  13

 

 

 

Leave a Comment