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?

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 compile function compiles a pattern of regular expression into an object of regular expression. Re.X is a flag which is also used as re.VERBOSE. Hence the output of this code is: re.compile(‘hello’, re.VERBOSE).
advertisement
advertisement

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

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

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

Answer: c
Explanation: The function re.split() splits the string on the basis of the pattern given in the parenthesis. Since we have used the flag e.I (that is, re.IGNORECASE), the output is: [‘0’, ‘3B6’].
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
re.sub('morning', 'evening', 'good morning')

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

Answer: a
Explanation: The code shown above first searches for the pattern ‘morning’ in the string ‘good morning’ and then replaces this pattern with ‘evening’. Hence the output of this code 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 function re.error raises an exception when a string passed to one of its functions here is not a valid regular expression. It does not raise an exception if a particular string does not contain a match for the given pattern.

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

re.escape('new**world')

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

Answer: b
Explanation: The function re.escape escapes all the characters in the pattern other than ASCII letters and numbers. Hence the output of the code shown above is: ‘new\\*\\*world’.

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

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 function re.fullmatch applies the pattern to the entire string and returns an object if match is found and none if match in not found. In the code shown above, match is not found. Hence there is no output.

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: The function re.L is also written as re.LOCALE. There is no function such as re.LOWERCASE in the re module of Python.
 
 

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 difference the functions re.sub and re.subn is that re.sub returns a string whereas re.subn returns a tuple.

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

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

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

Answer: b
Explanation: The code shown above splits the string based on the pattern given as an argument. Hence the output of the code is: [”, ‘bai*’].

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

re.findall('good', 'good is good')
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 function findall returns a list of all the non overlapping matches in a string. Hence the output of the first function is: [‘good’, ‘good’] and that of the second function is: [‘good’].
 
 

Sanfoundry Global Education & Learning Series – Python.

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

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.