Python MCQ – String – 5

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

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

print("abc DEF".capitalize())

a) abc def
b) ABC DEF
c) Abc def
d) Abc Def
View Answer

Answer: c
Explanation: The first letter of the string is converted to uppercase and the others are converted to lowercase.
advertisement
advertisement

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

print("abc. DEF".capitalize())

a) abc. def
b) ABC. DEF
c) Abc. def
d) Abc. Def
View Answer

Answer: c
Explanation: The first letter of the string is converted to uppercase and the others are converted to lowercase.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

advertisement
print("abcdef".center())

a) cd
b) abcdef
c) error
d) none of the mentioned
View Answer

Answer: c
Explanation: The function center() takes at least one parameter.
advertisement

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

print("abcdef".center(0))

a) cd
b) abcdef
c) error
d) none of the mentioned
View Answer

Answer: b
Explanation: The entire string is printed when the argument passed to center() is less than the length of the string.

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

print('*', "abcdef".center(7), '*')

a) * abcdef *
b) * abcdef *
c) *abcdef *
d) * abcdef*
View Answer

Answer: b
Explanation: Padding is done towards the left-hand-side first when the final string is of odd length. Extra spaces are present since we haven’t overridden the value of sep.

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

print('*', "abcdef".center(7), '*', sep='')

a) * abcdef  *
b) *  abcdef *
c) *abcdef *
d) * abcdef*
View Answer

Answer: d
Explanation: Padding is done towards the left-hand-side first when the final string is of odd length.

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

print('*', "abcde".center(6), '*', sep='')

a) * abcde  *
b) *  abcde *
c) *abcde *
d) * abcde*
View Answer

Answer: c
Explanation: Padding is done towards the right-hand-side first when the final string is of even length.

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

print("abcdef".center(7, 1))

a) 1abcdef
b) abcdef1
c) abcdef
d) error
View Answer

Answer: d
Explanation: TypeError, the fill character must be a character, not an int.

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

print("abcdef".center(7, '1'))

a) 1abcdef
b) abcdef1
c) abcdef
d) error
View Answer

Answer: a
Explanation: The character ‘1’ is used for padding instead of a space.

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

print("abcdef".center(10, '12'))

a) 12abcdef12
b) abcdef1212
c) 1212abcdef
d) error
View Answer

Answer: d
Explanation: The fill character must be exactly one character long.

Sanfoundry Global Education & Learning Series – Python.

To practice all basic questions 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.