Python MCQ – String – 11

«
»

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

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

print('Hello!2@#World'.istitle())

a) True
b) False
c) None
d) error
View Answer

Answer: a
Explanation: It is in the form of a title.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

2. What will be the output of the following Python code?

print('1Rn@'.lower())

a) n
b) 1rn@
c) rn
d) r
View Answer

Answer: b
Explanation: Uppercase letters are converted to lowercase. The other characters are left unchanged.

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

advertisement
print('''
 \tfoo'''.lstrip())

a) \tfoo
b) foo
c)   foo
d) none of the mentioned
View Answer

Answer: b
Explanation: All leading whitespace is removed.
advertisement

4. What will be the output of the following Python code?

print('xyyzxxyxyy'.lstrip('xyy'))

a) error
b) zxxyxyy
c) z
d) zxxy
View Answer

Answer: b
Explanation: The leading characters containing xyy are removed.

5. What will be the output of the following Python code?

print('xyxxyyzxxy'.lstrip('xyy'))

a) zxxy
b) xyxxyyzxxy
c) xyxzxxy
d) none of the mentioned
View Answer

Answer: a
Explanation: All combinations of the characters passed as an argument are removed from the left hand side.

6. What will be the output of the following Python code?

print('cba'.maketrans('abc', '123'))

a) {97: 49, 98: 50, 99: 51}
b) {65: 49, 66: 50, 67: 51}
c) 321
d) 123
View Answer

Answer: a
Explanation: A translation table is returned by maketrans.

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

print('a'.maketrans('ABC', '123'))

a) {97: 49, 98: 50, 99: 51}
b) {65: 49, 66: 50, 67: 51}
c) {97: 49}
d) 1
View Answer

Answer: b
Explanation: maketrans() is a static method so it’s behaviour does not depend on the object from which it is being called.

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

print('abcdef'.partition('cd'))

a) (‘ab’, ‘ef’)
b) (‘abef’)
c) (‘ab’, ‘cd’, ‘ef’)
d) 2
View Answer

Answer: c
Explanation: The string is split into three parts by partition.

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

print('abcdefcdgh'.partition('cd'))

a) (‘ab’, ‘cd’, ‘ef’, ‘cd’, ‘gh’)
b) (‘ab’, ‘cd’, ‘efcdgh’)
c) (‘abcdef’, ‘cd’, ‘gh’)
d) error
View Answer

Answer: b
Explanation: The string is partitioned at the point where the separator first appears.

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

print('abcd'.partition('cd'))

a) (‘ab’, ‘cd’, ”)
b) (‘ab’, ‘cd’)
c) error
d) none of the mentioned
View Answer

Answer: a
Explanation: The last item is a null string.

Sanfoundry Global Education & Learning Series – Python.

To practice all test questions online on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

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.