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
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).
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
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’.
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
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’.
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
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
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
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.LOWERCASEView Answer
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
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
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
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.
- Practice Programming MCQs
- Check Information Technology Books
- Check Python Books
- Apply for Programming Internship
- Apply for Python Internship