Python MCQ – String – 12

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

Answer: d
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’)
advertisement

So, the result is (”, ‘cd’, ”).

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

⚡ Claim Your Free Java Certification - December 2025
print('abef'.partition('cd'))

a) (‘abef’)
b) (‘abef’, ‘cd’, ”)
c) (‘abef’, ”, ”)
d) error
View Answer

Answer: c
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 → ”
advertisement

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

Answer: a
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

Answer: a
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

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

Answer: a
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

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

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

Answer: a
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

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

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.