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
Explanation: The first letter of the string is converted to uppercase and the others are converted to lowercase.
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
Explanation: The first letter of the string is converted to uppercase and the others are converted to lowercase.
3. What will be the output of the following Python code?
print("abcdef".center())
a) cd
b) abcdef
c) error
d) none of the mentioned
View Answer
Explanation: The function center() takes at least one parameter.
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
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
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
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
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
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
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
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.
- Apply for Python Internship
- Check Python Books
- Practice Programming MCQs
- Check Information Technology Books
- Apply for Programming Internship