Python MCQ – String – 13

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Strings – 13”.

1. 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
View Answer

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

2. 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’]
View Answer

Answer: a
Explanation: It is similar to calling split(‘\n’).
Note: Join free Sanfoundry classes at Telegram or Youtube

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

advertisement
print('Ab!2'.swapcase())

a) AB!@
b) ab12
c) aB!2
d) aB1@
View Answer

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

4. 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
View Answer

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

5. 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
View Answer

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

6. 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
View Answer

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

7. 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
View Answer

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

8. 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
View Answer

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

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

print('ab'.zfill(5))

a) 000ab
b) 00ab0
c) 0ab00
d) ab000
View Answer

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

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

print('+99'.zfill(5))

a) 00+99
b) 00099
c) +0099
d) +++99
View Answer

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

Sanfoundry Global Education & Learning Series – Python.

To practice entire question bank on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.