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
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
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
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.
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
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?
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
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?
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
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
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
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
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
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:
- Python Regular Expressions MCQ (Set 2)
- Python Regular Expressions MCQ (Set 3)
- Python Regular Expressions MCQ (Set 4)
- Python Regular Expressions MCQ (Set 5)
- Python Regular Expressions MCQ (Set 6)
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
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Python Internship
- Apply for Programming Internship