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!
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.
a) re
b) regex
c) reg
d) regexp
Answer: a
Explanation: The re
library is the standard library in Python for regular expressions.
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.
a) *
b) $
c) ?
d) .
Answer: d
Explanation: The .
(dot) character in regular expressions matches any single character, except newline characters.
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.
a) +
b) *
c) ?
d) {
Answer: c
Explanation: The ?
quantifier in regular expressions matches zero or one occurrence of the preceding pattern.
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.
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.
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.
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.
a) ^
b) $
c) *
d) +
Answer: a
Explanation: The ^
metacharacter in regular expressions matches the beginning of a line.
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.
a) !
b) &
c) \
d) /
Answer: c
Explanation: The \
(backslash) character is used to escape a metacharacter in regular expressions.
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.
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.
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.
a) ?
b) +
c) *
d) {
Answer: c
Explanation: The *
metacharacter in regular expressions matches zero or more occurrences of the preceding pattern.
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.
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.
a) \d
b) \w
c) .
d) \s
Answer: a
Explanation: The \d
metacharacter in regular expressions matches any digit from 0 to 9.
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.
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.
a) $
b) ^
c) .
d) *
Answer: a
Explanation: The $
metacharacter in regular expressions matches the end of a line.
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.
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.
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.
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.
a) \w
b) \s
c) .
d) +
Answer: b
Explanation: The \s
metacharacter in regular expressions matches any whitespace character, including spaces, tabs, and newlines.
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.
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.
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.
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.
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: