This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Strings – 11”.
1. What will be the output of the following Python code?
print('Hello!2@#World'.istitle())
a) True
b) False
c) None
d) error
View Answer
Explanation: It is in the form of a title.
2. What will be the output of the following Python code?
print('1Rn@'.lower())
a) n
b) 1rn@
c) rn
d) r
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: The last item is a null string.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Python Books
- Apply for Programming Internship
- Practice Programming MCQs
- Apply for Python Internship
- Check Information Technology Books