Python Questions and Answers – Regular Expressions – 2

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

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

import re
print(re.compile('hello', re.X))

a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b) re.compile(‘hello’, re.VERBOSE)
c) Error
d) Junk value
View Answer

Answer: b
Explanation: The flag re.X is a shorthand for re.VERBOSE, which allows you to write regular expressions more clearly with whitespace and comments. So, re.compile(‘hello’, re.X) is equivalent to re.compile(‘hello’, re.VERBOSE).
advertisement

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

import re
print(re.split('[a-c]', '0a3B6', re.I))

a) Error
b) [‘a’, ‘B’]
c) [‘0’, ‘3B6’]
d) [‘a’]
View Answer

Answer: c
Explanation: The re.split(‘[a-c]’, ‘0a3B6’, re.I) splits the string at any letter from ‘a’ to ‘c’, case-insensitively (due to re.I). So, it splits at ‘a’, resulting in parts: ‘0’ and ‘3B6’.
Free 30-Day C Certification Bootcamp is Live. Join Now!

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

import re
print(re.sub('morning', 'evening', 'good morning'))

a) ‘good evening’
b) ‘good’
c) ‘morning’
d) ‘evening’
View Answer

Answer: a
Explanation: The re.sub() function replaces all occurrences of the pattern ‘morning’ with ‘evening’ in the given string ‘good morning’. Hence, the output is ‘good evening’.
advertisement

4. The function re.error raises an exception if a particular string contains no match for the given pattern.
a) True
b) False
View Answer

Answer: b
Explanation: The re.error exception is raised for invalid regex patterns, not for when there is no match. If no match is found, functions like re.search() simply return None without raising an error.

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

import re
print(re.escape('new**world'))

a) ‘new world’
b) ‘new\\*\\*world’
c) ‘**’
d) ‘new’, ‘*’, ‘*’, ‘world’
View Answer

Answer: b
Explanation: The re.escape() function escapes all non-alphanumeric characters in the string by adding backslashes before them. So the asterisks ** are escaped as \*\*, resulting in ‘new\\*\\*world’.

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

import re
print(re.fullmatch('hello', 'hello world'))

a) No output
b) []
c) <_sre.SRE_Match object; span=(0, 5), match=’hello’>
d) Error
View Answer

Answer: a
Explanation: The re.fullmatch() function attempts to match the entire string against the given pattern. In the code:

re.fullmatch('hello', 'hello world')

The string ‘hello world’ does not exactly match the pattern ‘hello’ because it has extra characters (‘ world’). Hence, re.fullmatch() returns None, which produces no output when printed.

7. Choose the option wherein the two choices do not refer to the same option.
a)

   re.I
   re.IGNORECASE

b)

   re.M
   re.MULTILINE

c)

   re.X
   re.VERBOSE

d)

   re.L
   re.LOWERCASE
View Answer
Answer: d
Explanation: Options like re.I and re.IGNORECASE, re.M and re.MULTILINE, and re.X and re.VERBOSE are equivalent pairs in Python’s re module—they are just aliases for the same flags. However, re.LOWERCASE does not exist in the re module, while re.L is a valid flag used for locale-dependent matching.
 
 

8. The difference between the functions re.sub and re.subn is that re.sub returns a _______________ whereas re.subn returns a __________________
a) string, list
b) list, tuple
c) string, tuple
d) tuple, list
View Answer

Answer: c
Explanation: The re.sub() function returns just the modified string after all replacements. In contrast, re.subn() returns a tuple containing the modified string and the number of substitutions made. This helps track how many replacements occurred.

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

import re
print(re.split('mum', 'mumbai*', 1))

a) Error
b) [”, ‘bai*’]
c) [”, ‘bai’]
d) [‘bai*’]
View Answer

Answer: b
Explanation: The re.split(‘mum’, ‘mumbai*’, 1) function splits the string ‘mumbai*’ at the first occurrence of the pattern ‘mum’. Since ‘mum’ occurs at the start, the string is split into two parts: an empty string before ‘mum’ and ‘bai*’ after it. Hence, the output is [”, ‘bai*’].

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

import re
print(re.findall('good', 'good is good'))
print(re.findall('good', 'bad is good'))

a)

[‘good’, ‘good’]
[‘good’]

b)

(‘good’, ‘good’)
(good)

c)

(‘good’)
(‘good’)

d)

[‘good’]
[‘good’]
View Answer
Answer: a
Explanation: The re.findall() function returns a list of all matches of the given pattern in the string. In the first call, ‘good’ appears twice in ‘good is good’, so it returns [‘good’, ‘good’]. In the second call, ‘good’ appears once in ‘bad is good’, so it returns [‘good’].
 
 

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.