Our Features

Our Features
.

Friday, 17 June 2016

Python Questions and Answers – Strings – 5

This set of Basic Python Questions & Answers focuses on “Strings”.
1. What is the output of the following?
print("abc DEF".capitalize())
a) abc def
b) ABC DEF
c) Abc def
d) Abc Def
View Answer
2. What is the output of the following?
print("abc. DEF".capitalize())
a) abc def
b) ABC DEF
c) Abc def
d) Abc Def
View Answer
3. What is the output of the following?
print("abcdef".center())
a) cd
b) abcdef
c) error
d) none of the mentioned
View Answer
4. What is the output of the following?
print("abcdef".center(0))
advertisements
a) cd
b) abcdef
c) error
d) none of the mentioned
View Answer
5. What is the output of the following?
print('*', "abcdef".center(7), '*')
a) * abcdef *
b) * abcdef *
c) *abcdef *
d) * abcdef*
View Answer
6. What is the output of the following?
print('*', "abcdef".center(7), '*', sep='')
a) * abcdef *
b) * abcdef *
c) *abcdef *
d) * abcdef*
View Answer
7. What is the output of the following?
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.
advertisements
8. What is the output of the following?
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 is the output of the following?
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 is the output of the following?
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

No comments:

Post a Comment