Python Multiple Choice Questions – Regular Expressions

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Regular Expressions”.

1. Which module in Python supports regular expressions?
a) re
b) regex
c) pyregex
d) none of the mentioned
View Answer

Answer: a
Explanation: The re module in Python provides support for regular expressions, enabling pattern matching and text manipulation. It is the standard library module used for regex operations in Python.

2. Which of the following creates a pattern object?
a) re.create(str)
b) re.regex(str)
c) re.compile(str)
d) re.assemble(str)
View Answer

Answer: c
Explanation: The function re.compile(str) compiles a regular expression pattern into a pattern object, which can then be used for matching using its methods like .match(), .search(), etc. This improves performance if the same pattern is used multiple times.

3. What does the function re.match do?
a) matches a pattern at the start of the string
b) matches a pattern at any position in the string
c) such a function does not exist
d) none of the mentioned
View Answer

Answer: a
Explanation: re.match() checks for a match only at the beginning of the string. If the pattern is found right at the start, it returns a match object; otherwise, it returns None. For matching patterns anywhere in the string, re.search() is used instead.
advertisement

4. What does the function re.search do?
a) matches a pattern at the start of the string
b) matches a pattern at any position in the string
c) such a function does not exist
d) none of the mentioned
View Answer

Answer: b
Explanation: re.search() scans through the entire string and returns a match object for the first occurrence of the pattern anywhere in the string. Unlike re.match(), it does not require the pattern to be at the start.

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

Free 30-Day Python Certification Bootcamp is Live. Join Now!
import re
sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.groups())

a) (‘we’, ‘are’, ‘humans’)
b) (we, are, humans)
c) (‘we’, ‘humans’)
d) ‘we are humans’
View Answer

Answer: a
Explanation: The regex pattern (.*) (.*?) (.*) breaks the sentence into three groups separated by spaces:

  • (.*) — matches any characters greedily until the last space (captures ‘we’ in this case because of the lazy second group),
  • (.*?) — matches any characters lazily until the next space (captures ‘are’),
  • (.*) — matches the rest of the string (captures ‘humans’).

So, matched.groups() returns a tuple of three strings: (‘we’, ‘are’, ‘humans’).

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

advertisement
import re
sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.group())

a) (‘we’, ‘are’, ‘humans’)
b) (we, are, humans)
c) (‘we’, ‘humans’)
d) we are humans
View Answer

Answer: d
Explanation: matched.group() returns the entire matched portion of the string, which is “we are humans”. To get the individual parts captured by the groups, you would use matched.groups() or specify group numbers like matched.group(1).

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

import re
sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.group(2))

a) ‘are’
b) ‘we’
c) ‘humans’
d) ‘we are humans’
View Answer

Answer: a
Explanation: The regex splits the sentence into three groups separated by spaces. The second group (.*?) captures the word ‘are’, so matched.group(2) returns ‘are’.

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

import re
sentence = 'horses are fast'
regex = re.compile('(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)')
matched = re.search(regex, sentence)
print(matched.groupdict())

a) {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
b) (‘horses’, ‘are’, ‘fast’)
c) ‘horses are fast’
d) ‘are’
View Answer

Answer: a
Explanation: The output is {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’} because the regex uses named groups (?P<name>) to capture words in the sentence. The groupdict() method returns a dictionary mapping the group names to their matched substrings.

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

import re
sentence = 'horses are fast'
regex = re.compile('(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)')
matched = re.search(regex, sentence)
print(matched.groups())

a) {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
b) (‘horses’, ‘are’, ‘fast’)
c) ‘horses are fast’
d) ‘are’
View Answer

Answer: b
Explanation: matched.groups() returns a tuple of all matched groups without their names. So it outputs (‘horses’, ‘are’, ‘fast’) as a simple tuple of strings. Named groups only affect groupdict(), not groups().

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

import re
sentence = 'horses are fast'
regex = re.compile('(?P<animal>\w+) (?P<verb>\w+) (?P<adjective>\w+)')
matched = re.search(regex, sentence)
print(matched.group(2))

a) {‘animal’: ‘horses’, ‘verb’: ‘are’, ‘adjective’: ‘fast’}
b) (‘horses’, ‘are’, ‘fast’)
c) ‘horses are fast’
d) ‘are’
View Answer

Answer: d
Explanation: The regex captures three words as named groups: ‘animal’, ‘verb’, and ‘adjective’. Using matched.group(2) returns the second captured group, which corresponds to ‘are’.

More MCQs on Python Regular Expressions:

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest 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 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.