30+ MCQs on Python Regular Expression

Ayushi Trivedi Last Updated : 06 Mar, 2024
7 min read

Welcome to the Python Regular Expression Python Interview Questions! Regular expressions, often abbreviated as “regex,” are powerful tools for pattern matching and searching in strings. In Python, the re module provides support for regular expressions, allowing you to search, match, and manipulate text based on specific patterns. These questions will test your understanding of various aspects of regular expressions in Python, including pattern matching, special characters, flags, methods, and more. Each question is multiple-choice, with only one correct answer. Take your time to carefully read each question and choose the best option. Let’s dive into the world of Python regular expressions together!

Python Regular Expression

30+ Python Interview Questions on Python Regular Expression

Q1. What is a regular expression in Python?

a) A function that creates strings

b) A sequence of characters that forms a search pattern

c) A method for sorting lists

d) An exception type in Python

Answer: b

Explanation: A regular expression is a sequence of characters that forms a search pattern, used mainly for pattern matching with strings.

Q2. Which library in Python is used for regular expressions?

a) re

b) regex

c) reg

d) regexp

Answer: a

Explanation: The re library is the standard library in Python for regular expressions.

Q3. What does the re.search() function in Python do?

a) Searches for a pattern within a string

b) Sorts a list of strings

c) Converts a string to lowercase

d) Compares two strings

Answer: a

Explanation: The re.search() function searches for a pattern within a string and returns the first match.

Q4. Which character is used in regular expressions to match any single character?

a) *

b) $

c) ?

d) .

Answer: d

Explanation: The . (dot) character in regular expressions matches any single character, except newline characters.

Q5. What will the following regular expression pattern match?

pattern = r'\d+'

a) Matches one or more digits

b) Matches one or more alphabetic characters

c) Matches any character

d) Matches a specific string

Answer: a

Explanation: The pattern \d+ matches one or more digits in a string.

Q6. Which of the following quantifiers in regular expressions matches zero or one occurrence of the preceding pattern?

a) +

b) *

c) ?

d) {

Answer: c

Explanation: The ? quantifier in regular expressions matches zero or one occurrence of the preceding pattern.

Q7. What will the following regular expression pattern match?

pattern = r'\w{3}'

a) Matches exactly three alphanumeric characters

b) Matches any word character

c) Matches any three characters

d) Matches three digits

Answer: a

Explanation: The pattern \w{3} matches exactly three alphanumeric characters.

Q8. What does the re.findall() function in Python do?

a) Finds all matches of a pattern within a string and returns them as a list

b) Finds the first match of a pattern within a string

c) Replaces all occurrences of a pattern with a specified string

d) Returns the length of the matching substring

Answer: a

Explanation: The re.findall() function finds all matches of a pattern within a string and returns them as a list.

Q9. Which of the following is a valid regular expression to match the word “hello” in a string?

a) r’hello’

b) ‘hello’

c) r’\bhello\b’

d) ‘hello’

Answer: c

Explanation: The regular expression \bhello\b matches the word “hello” as a whole word in a string.

Q10. What will be the output of the following code?

import re

pattern = r'\d{2}-\d{2}-\d{4}'
text = "Date of birth: 12-31-1990"
result = re.findall(pattern, text)
print(result)

a) [’12-31-1990′]

b) [’12’, ’31’, ‘1990’]

c) [’12’, ‘-‘, ’31’, ‘-‘, ‘1990’]

d) []

Answer: a

Explanation: The code will output ['12-31-1990'] because the regular expression matches the date format within the given text.

Q11. Which of the following metacharacters in regular expressions matches the beginning of a line?

a) ^

b) $

c) *

d) +

Answer: a

Explanation: The ^ metacharacter in regular expressions matches the beginning of a line.

Q12. What does the re.sub() function in Python do?

a) Searches for a pattern within a string and returns the first match

b) Finds all matches of a pattern within a string and returns them as a list

c) Replaces all occurrences of a pattern with a specified string

d) Splits a string into a list based on a pattern

Answer: c

Explanation: The re.sub() function in Python replaces all occurrences of a pattern with a specified string in a given text.

Q13. Which of the following characters is used to escape a metacharacter in regular expressions?

a) !

b) &

c) \

d) /

Answer: c

Explanation: The \ (backslash) character is used to escape a metacharacter in regular expressions.

Q14. What will the following regular expression pattern match?

pattern = r'\b[A-Z]\w+\b'

a) Matches words that start with an uppercase letter

b) Matches any word character

c) Matches any three characters

d) Matches words that start with a lowercase letter

Answer: a

Explanation: The pattern \b[A-Z]\w+\b matches words that start with an uppercase letter followed by one or more word characters.

Q15. Which of the following is NOT a valid flag for the re.compile() function in Python?

a) re.IGNORECASE

b) re.MULTILINE

c) re.ALLCAPS

d) re.DOTALL

Answer: c

Explanation: re.ALLCAPS is not a valid flag for the re.compile() function in Python. The correct flag is re.IGNORECASE for case-insensitive matching.

Q16. What will be the output of the following code?

import re

pattern = r'[A-Za-z0-9_]+'
text = "Hello World_123"
result = re.findall(pattern, text)
print(result)

a) [‘Hello’, ‘World_123’]

b) [‘Hello’, ‘World’, ‘123’]

c) [‘Hello_World_123’]

d) [‘Hello’, ‘World’, ‘_’, ‘123’]

Answer: a

Explanation: The code will output ['Hello', 'World_123'] because the regular expression matches alphanumeric characters and underscores.

Q17. Which of the following metacharacters in regular expressions matches zero or more occurrences of the preceding pattern?

a) ?

b) +

c) *

d) {

Answer: c

Explanation: The * metacharacter in regular expressions matches zero or more occurrences of the preceding pattern.

Q18. What does the following regular expression pattern match?

pattern = r'^[a-z]+'

a) Matches one or more lowercase letters at the beginning of a line

b) Matches any lowercase letter

c) Matches any character at the beginning of a line

d) Matches one or more lowercase letters

Answer: a

Explanation: The pattern ^[a-z]+ matches one or more lowercase letters at the beginning of a line.

Q19. What will be the output of the following code?

import re

pattern = r'\b\d{3}\b'
text = "123 4567 890 12 3456"
result = re.findall(pattern, text)
print(result)

a) [‘123’, ‘890’]

b) [‘123’, ‘456’, ‘890’, ‘345’]

c) [‘123’, ‘890’, ‘345’]

d) [‘123’]

Answer: a

Explanation: The code will output ['123', '890'] because the pattern \b\d{3}\b matches three-digit numbers as whole words.

Q20. Which of the following metacharacters in regular expressions matches any digit?

a) \d

b) \w

c) .

d) \s

Answer: a

Explanation: The \d metacharacter in regular expressions matches any digit from 0 to 9.

Q21. What will the following regular expression pattern match?

pattern = r'\b[A-Z]\w*'

a) Matches words that start with an uppercase letter

b) Matches any word character

c) Matches any three characters

d) Matches words that start with a lowercase letter

Answer: a

Explanation: The pattern \b[A-Z]\w* matches words that start with an uppercase letter followed by zero or more word characters.

Q22. What is the purpose of the re.compile() function in Python regular expressions?

a) To compile Python code

b) To define a regular expression pattern

c) To create a compiled regular expression object for reuse

d) To execute a regular expression pattern

Answer: c

Explanation: The re.compile() function in Python is used to create a compiled regular expression object for reuse, which can improve performance.

Q23. Which of the following metacharacters in regular expressions matches the end of a line?

a) $

b) ^

c) .

d) *

Answer: a

Explanation: The $ metacharacter in regular expressions matches the end of a line.

Q24. What does the re.split() function in Python do?

a) Searches for a pattern within a string and returns the first match

b) Finds all matches of a pattern within a string and returns them as a list

c) Replaces all occurrences of a pattern with a specified string

d) Splits a string into a list based on a pattern

Answer: d

Explanation: The re.split() function in Python splits a string into a list based on a pattern.

Q25. What will be the output of the following code?

import re

pattern = r'\b[A-Za-z]+\b'
text = "Hello World123"
result = re.findall(pattern, text)
print(result)

a) [‘Hello’, ‘World’]

b) [‘Hello’, ‘World123’]

c) [‘Hello’, ‘World123’]

d) [‘Hello’]

Answer: a

Explanation: The code will output ['Hello', 'World'] because the regular expression matches words containing only letters.

Q26. Which of the following is a valid regular expression to match an email address?

a) r’\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}\b’

b) r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}’

c) r’\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}’

d) r’\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,3}\b’

Answer: a

Explanation: The regular expression \b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}\b is a valid pattern to match an email address.

Q27. What will be the output of the following code?

import re

pattern = r'\b\d{3}-\d{2}-\d{4}\b'
text = "Social Security Numbers: 123-45-6789, 9876-543-210"
result = re.findall(pattern, text)
print(result)

a) [‘123-45-6789’]

b) [‘123′, ’45’, ‘6789’]

c) [‘123-45-6789’, ‘9876-543-210’]

d) [‘123′, ’45’, ‘6789’, ‘9876’, ‘543’, ‘210’]

Answer: a

Explanation: The code will output ['123-45-6789'] because the regular expression matches the Social Security Number format within the given text.

Q28. Which of the following metacharacters in regular expressions matches any whitespace character?

a) \w

b) \s

c) .

d) +

Answer: b

Explanation: The \s metacharacter in regular expressions matches any whitespace character, including spaces, tabs, and newlines.

Q29. What does the re.match() function in Python do?

a) Searches for a pattern within a string and returns the first match

b) Finds all matches of a pattern within a string and returns them as a list

c) Replaces all occurrences of a pattern with a specified string

d) Checks if a pattern matches at the beginning of a string

Answer: d

Explanation: The re.match() function in Python checks if a pattern matches at the beginning of a string.

Q30. What will be the output of the following code?

import re

pattern = r'\b\d{3}\b'
text = "123 4567 890 12 3456"
result = re.findall(pattern, text)
print(result)

a) [‘123’, ‘890’]

b) [‘123’, ‘456’, ‘890’, ‘345’]

c) [‘123’, ‘890’, ‘345’]

d) [‘123’]

Answer: a

Explanation: The code will output ['123', '890'] because the pattern \b\d{3}\b matches three-digit numbers as whole words.

Q31. What will be the output of the following code?

import re

pattern = r'\b[A-Z][a-z]*\b'
text = "The quick Brown Fox jumps over the Lazy Dog"
result = re.findall(pattern, text)
print(result)

a) [‘The’, ‘Brown’, ‘Fox’, ‘Lazy’, ‘Dog’]

b) [‘The’, ‘Fox’, ‘Lazy’, ‘Dog’]

c) [‘The’, ‘quick’, ‘Brown’, ‘Fox’, ‘Lazy’, ‘Dog’]

d) [‘The’, ‘quick’, ‘Fox’, ‘Dog’]

Answer: a

Explanation: The code will output ['The', 'Brown', 'Fox', 'Lazy', 'Dog'] because the pattern \b[A-Z][a-z]*\b matches words starting with an uppercase letter in the given text.

Q32. Which of the following is a valid regular expression to match a valid email address?

a) r’\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Z|a-z]{2,}\b’

b) r’\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,}\b’

c) r’\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,3}\b’

d) r’\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}\b’

Answer: d

Explanation: The regular expression r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b' is a valid pattern to match a valid email address.

Q33. What will be the output of the following code?

import re

pattern = r'\b\w{4,}\b'
text = "Python is a powerful programming language"
result = re.findall(pattern, text)
print(result)

a) [‘Python’, ‘powerful’, ‘programming’, ‘language’]

b) [‘Python’, ‘powerful’, ‘programming’]

c) [‘powerful’, ‘programming’, ‘language’]

d) [‘Python’, ‘programming’, ‘language’]

Answer: a

Explanation: The code will output ['Python', 'powerful', 'programming', 'language'] because the pattern \b\w{4,}\b matches words with four or more letters in the given text.

Congratulations on completing the Python Regular Expression MCQs! Regular expressions are essential for advanced string manipulation and pattern matching tasks in Python. By mastering regular expressions, you gain the ability to search for specific patterns, extract information, validate input, and perform complex text manipulation operations. Keep practicing and experimenting with different patterns and methods provided by the re module to become proficient in using regular expressions effectively. If you have any questions or want to delve deeper into any topic, don’t hesitate to continue your learning journey. Happy coding!

You can also enroll in our free Python Course Today!

Read our more articles related to MCQs in Python:

My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more. I am also an author. My first book named #turning25 has been published and is available on amazon and flipkart. Here, I am technical content editor at Analytics Vidhya. I feel proud and happy to be AVian. I have a great team to work with. I love building the bridge between the technology and the learner.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details