Python MCQs on String: SET 11

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

print(‘[email protected]#World’.istitle())

a) True

b) False

c) None

d) error

Check Answer

Answer: a
It is in the form of a title.

 

 

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

print(‘[email protected]’.lower())

a) n

b) [email protected]

c) rn

d) r

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

 

 

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

print(”’\tfoo”’.lstrip())

a) \tfoo

b) foo

c)   foo

d) none of the mentioned

Check Answer

Answer: b
All leading whitespace is removed.

 

 

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

print(‘xyyzxxyxyy’.lstrip(‘xyy’))

a) error

b) zxxyxyy

c) z

d) zxxy

Check Answer

Answer: b
The leading characters containing xyy are removed.

 

 

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

Check Answer

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

 

 

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

Check Answer

 

Answer: a
A translation table is returned by maketrans.

 

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

Check Answer

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

 

 

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

Check Answer

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

 

 

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

Check Answer

 

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

 

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

Check Answer

Answer: a
The last item is a null string.

 

 

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

 

 

 

Leave a Comment