Python Quizs Question– String II


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

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

A. AB!@
B. ab12
C. aB1@
D. aB!2

Show Answer Answer:-D. aB!2
Explanation Lowercase letters are converted to uppercase and vice-versa.

Q 2. 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

Show Answer Answer:-C. Ab Cd Ef
Explanation The first letter of every word is capitalized.

Q 3. 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

Show Answer Answer:-B. Ab Cd-Ef
Explanation The first letter of every word is capitalized. Special symbols terminate a word.

Q 4 . 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

Show Answer Answer:-D. bcdd
Explanation The output is bcdd since no translation is provided for d.

Q 5. 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

Show Answer Answer:-D. none of the mentioned
Explanation The output is bcdd since no translation is provided for d.

Q 6. 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

Show Answer Answer:-A. abcd
Explanation The function translate expects a dictionary of integers. Use maketrans() instead of doing the above.

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

print(‘ab’.zfill(5))

A. 000ab0
B. 00ab
C. 0ab00
D. ab000

Show Answer Answer:-B. 00ab
Explanation The string is padded with zeros on the left hand side. It is useful for formatting numbers.

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

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

A. 00+99
B. 00099
C. +0099
D. +++99

Show Answer Answer:-C. +0099
Explanation zeros are filled in between the first sign and the rest of the string.

Q 9. What will be the output of the following Python code snippet?print(‘ab\ncd\nef’.splitlines())

A. [‘ab\n’, ‘cd\n’, ‘ef’]
B. [‘ab\n’, ‘cd\n’, ‘ef\n’]
C. [‘ab’, ‘cd’, ‘ef’]
D. [‘ab’, ‘cd’, ‘ef\n’]

Show Answer Answer:-C. [‘ab’, ‘cd’, ‘ef’]
Explanation It is similar to calling split(‘\n’).

Q 10 . What will be the output of the following Python code snippet?print(‘abcdefcdghcd’.split(‘cd’, 2))

A. [‘ab’, ‘efcdghcd’]
B. [‘ab’, ‘ef’, ‘ghcd’]
C. [‘abcdef’, ‘ghcd’]
D. none of the mentioned

Show Answer Answer:-B. [‘ab’, ‘ef’, ‘ghcd’]
Explanation The string is split into a maximum of maxsplit+1 substrings.

Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page