Python MCQs on Regular Expressions: Set 1

Python MCQs on Regular Expressions

In this section, we are going to introduce you to the most frequently asked Python MCQs on Regular Expressions. These Python MCQs on Regular Expressions will be going to help you in Interviews and Exams.

 

1. Which module in Python supports regular expressions?

a) re

b) regex

c) pyregex

d) none of the mentioned

Check Answer

Answer: a
re is a part of the standard library and can be imported using: import re.

 

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)

Check Answer

Answer: c
It converts a given string into a pattern object.

 

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

Check Answer

Answer: a
It will look for the pattern at the beginning and return None if it isn’t found.

 

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

Check Answer

Answer: b
It will look for the pattern at any position in the string.

 

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

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’

Check Answer

Answer: a
This function returns all the subgroups that have been matched.

 

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

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’

Check Answer

Answer: d
This function returns the entire match.

 

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

sentence = ‘we are humans’

matched = re.match(r'(.*) (.*?) (.*)’, sentence)

print(matched.group(2))

a) ‘are’

b) ‘we’

c) ‘humans’

d) ‘we are humans’

Check Answer

Answer: c
This function returns the particular subgroup.

 

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

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’

Check Answer

Answer: a
This function returns a dictionary that contains all the matches.

 

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

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’

Check Answer

Answer: b
This function returns all the subgroups that have been matched.

 

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

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’

Check Answer

Answer: d
This function returns the particular subgroup.

Page  1  2  3  4  5  6

 

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

 

Leave a Comment