Python MCQs on String: SET 5

41. 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

Check Answer

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

 

 

42. 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

Check Answer

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

 

 

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

print(“abcdef”.center())

a) cd

b) abcdef

c) error

d) none of the mentioned

Check Answer

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

 

 

44. 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

Check Answer

 

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

 

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

print(‘*’, “abcdef”.center(7), ‘*’)

a) * abcdef *

b) * abcdef *

c) *abcdef *

d) * abcdef*

Check Answer

Answer: b
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.

 

 

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

print(‘*’, “abcdef”.center(7), ‘*’, sep=”)

a) * abcdef *

b) * abcdef *

c) *abcdef *

d) * abcdef*

Check Answer

 

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

 

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

print(‘*’, “abcde”.center(6), ‘*’, sep=”)

a) * abcde *

b) * abcde *

c) *abcde *

d) * abcde*

Check Answer

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

 

 

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

print(“abcdef”.center(7, 1))

a) 1abcdef

b) abcdef1

c) abcdef

d) error

Check Answer

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

 

 

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

print(“abcdef”.center(7, ‘1’))

a) 1abcdef

b) abcdef1

c) abcdef

d) error

Check Answer

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

 

 

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

print(“abcdef”.center(10, ’12’))

a) 12abcdef12

b) abcdef1212

c) 1212abcdef

d) error

Check Answer

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

 

Pages  1  2  3  4  5  6  7  8  9  10  11  12  13

 

 

 

 

Leave a Comment