This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Strings – 12”.
1. What will be the output of the following Python code snippet?
print('cd'.partition('cd'))
a) (‘cd’)
b) (”)
c) (‘cd’, ”, ”)
d) (”, ‘cd’, ”)
View Answer
Explanation: When using .partition(‘cd’) on the string ‘cd’, the separator ‘cd’ matches the entire string. The .partition() method returns a tuple:
- The part before the match → ” (nothing before ‘cd’)
- The separator itself → ‘cd’
- The part after the match → ” (nothing after ‘cd’)
So, the result is (”, ‘cd’, ”).
2. What will be the output of the following Python code snippet?
print('abef'.partition('cd'))
a) (‘abef’)
b) (‘abef’, ‘cd’, ”)
c) (‘abef’, ”, ”)
d) error
View Answer
Explanation: When the separator ‘cd’ is not found in the string ‘abef’, the .partition() method returns a tuple:
- The original string as the first element → ‘abef’
- An empty string as the second element (no separator found) → ”
- Another empty string as the third element → ”
So, the output is (‘abef’, ”, ”).
3. What will be the output of the following Python code snippet?
print('abcdef12'.replace('cd', '12'))
a) ab12ef12
b) abcdef12
c) ab12efcd
d) none of the mentioned
View Answer
Explanation: The .replace(‘cd’, ’12’) method replaces all occurrences of ‘cd’ with ’12’ in the string ‘abcdef12’. Since ‘cd’ appears once, it is replaced, resulting in ‘ab12ef12’. The method processes the string from left to right.
4. What will be the output of the following Python code snippet?
print('abef'.replace('cd', '12'))
a) abef
b) 12
c) error
d) none of the mentioned
View Answer
Explanation: The .replace(‘cd’, ’12’) method looks for the substring ‘cd’ in ‘abef’, but since it is not found, the original string is returned unchanged. No error is raised.
5. What will be the output of the following Python code snippet?
print('abcefd'.replace('cd', '12'))
a) ab1ef2
b) abcefd
c) ab1efd
d) ab12ed2
View Answer
Explanation: The .replace(‘cd’, ’12’) method attempts to find the substring ‘cd’ in ‘abcefd’. Since ‘cd’ does not exist in the string, no replacement is made, and the original string ‘abcefd’ is returned unchanged.
6. What will be the output of the following Python code snippet?
print('xyyxyyxyxyxxy'.replace('xy', '12', 0))
a) xyyxyyxyxyxxy
b) 12y12y1212x12
c) 12yxyyxyxyxxy
d) xyyxyyxyxyx12
View Answer
Explanation: The .replace(old, new, count) method replaces at most count occurrences of old with new. If the count is 0, it means replace zero occurrences, so no replacements are made. In this case, ‘xyyxyyxyxyxxy’.replace(‘xy’, ’12’, 0) results in the original string being returned unchanged: ‘xyyxyyxyxyxxy’.
7. What will be the output of the following Python code snippet?
print('xyyxyyxyxyxxy'.replace('xy', '12', 100))
a) xyyxyyxyxyxxy
b) 12y12y1212x12
c) none of the mentioned
d) error
View Answer
Explanation: The .replace(‘xy’, ’12’, 100) method replaces up to 100 occurrences of ‘xy’ with ’12’. Since there are fewer than 100 matches in the string ‘xyyxyyxyxyxxy’, all occurrences of ‘xy’ are replaced.
Here’s how it works step-by-step:
- ‘xyy’ → ’12y’
- ‘xyy’ → ’12y’
- next ‘xy’ → ’12’
- next ‘xy’ → ’12’
- ‘xxy’ → only one ‘xy’ is replaced → ‘x12’
Final result: ’12y12y1212x12′
8. What will be the output of the following Python code snippet?
print('abcdefcdghcd'.split('cd'))
a) [‘ab’, ‘ef’, ‘gh’]
b) [‘ab’, ‘ef’, ‘gh’, ”]
c) (‘ab’, ‘ef’, ‘gh’)
d) (‘ab’, ‘ef’, ‘gh’, ”)
View Answer
Explanation: The .split(‘cd’) method divides the string ‘abcdefcdghcd’ at each occurrence of ‘cd’. This results in four parts: ‘ab’, ‘ef’, ‘gh’, and an empty string ” at the end because the string ends with ‘cd’. Therefore, the output is [‘ab’, ‘ef’, ‘gh’, ”].
9. What will be the output of the following Python code snippet?
print('abcdefcdghcd'.split('cd', 0))
a) [‘abcdefcdghcd’]
b) ‘abcdefcdghcd’
c) error
d) none of the mentioned
View Answer
Explanation: The .split(‘cd’, 0) method tells Python to split the string at 0 occurrences of the substring ‘cd’. Since the count is zero, no splitting happens, and the entire original string is returned as a single-element list: [‘abcdefcdghcd’].
10. What will be the output of the following Python code snippet?
print('abcdefcdghcd'.split('cd', -1))
a) [‘ab’, ‘ef’, ‘gh’]
b) [‘ab’, ‘ef’, ‘gh’, ”]
c) (‘ab’, ‘ef’, ‘gh’)
d) (‘ab’, ‘ef’, ‘gh’, ”)
View Answer
Explanation: When .split(‘cd’, -1) is called with a negative maxsplit, it behaves the same as if maxsplit was not specified at all. This means the string ‘abcdefcdghcd’ is split at every occurrence of ‘cd’, resulting in the list [‘ab’, ‘ef’, ‘gh’, ”]. The last element is an empty string because the string ends with ‘cd’.
Sanfoundry Global Education & Learning Series – Python.
To practice all problems on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Programming Internship
- Check Information Technology Books
- Apply for Python Internship
- Practice Programming MCQs
- Check Python Books