250+ TOP MCQs on Python Strings and Answers

Online Python Test on “Strings”.

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

a) True
b) False
c) None
d) error
Answer: a
Clarification: It is in the form of a title.

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

a) n
b) [email protected]
c) rn
d) r
Answer: b
Clarification: Uppercase letters are converted to lowercase. The other characters are left unchanged.

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

print('''
 tfoo'''.lstrip())

a) tfoo
b) foo
c)   foo
d) none of the mentioned
Answer: b
Clarification: All leading whitespace is removed.

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

print('xyyzxxyxyy'.lstrip('xyy'))

a) error
b) zxxyxyy
c) z
d) zxxy
Answer: b
Clarification: The leading characters containing xyy are removed.

5. 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
Answer: a
Clarification: All combinations of the characters passed as an argument are removed from the left hand side.

6. 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
Answer: a
Clarification: A translation table is returned by maketrans.

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

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

8. 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
Answer: c
Clarification: The string is split into three parts by partition.

9. 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
Answer: b
Clarification: The string is partitioned at the point where the separator first appears.

10. 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
Answer: a
Clarification: The last item is a null string.

test questions online on Python,

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top