LearnFrenzy
Sign in Take a free test

Regular Expressions · Exercise 2 of 4

Regular Expressions Predict the Output

  • 15 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each Python program and choose what it prints. Every program was run on Python 3, so the answers are the real output.

Question 1 Easy

What is the output of the following Python program?

main.py
import re
s = "Order 42 shipped 7 days ago"
print(re.search(r"\d+", s).group(), re.findall(r"\d+", s))
A 42 [42, 7]
B 42 ['42', '7']
C 4 ['4', '2', '7']
D ['42'] ['42', '7']
Question page & discussion
Question 2 Easy

What is the output of the following Python program?

main.py
import re
print(re.match(r"\d", "a1"), bool(re.search(r"\d", "a1")), re.match(r"a", "a1").group())
A a1 True a
B None True a
C None False a
D 1 True a
Question page & discussion
Question 3 Easy

What is the output of the following Python program?

main.py
import re
for s in ["9845012345", "98450 12345", "98450123456"]:
    print(bool(re.fullmatch(r"\d{10}", s)), end=" ")
print(bool(re.search(r"\d{10}", "98450123456")))
A True False False True
B True True False True
C True False True True
D True False False False
Question page & discussion
Question 4 Medium

What is the output of the following Python program?

main.py
import re
m = re.search(r"(\w+)@(\w+)\.com", "mail asha@lf.com now")
print(m.group(0), m.group(1), m.groups(), m.span())
A asha@lf.com asha ('asha@lf.com', 'asha', 'lf') (5, 16)
B asha@lf.com asha ('asha', 'lf') (5, 16)
C asha asha ('asha', 'lf') (5, 16)
D asha@lf.com lf ('asha', 'lf') (5, 15)
Question page & discussion
Question 5 Medium

What is the output of the following Python program?

main.py
import re
m = re.search(r"(?P<day>\d{2})-(?P<mon>\d{2})-(?P<yr>\d{4})", "Born 07-03-2004")
print(m["yr"], m.group("day"), m.groupdict())
A 2004 7 {'day': 7, 'mon': 3, 'yr': 2004}
B 07 2004 {'yr': '2004'}
C 2004 07 ('07', '03', '2004')
D 2004 07 {'day': '07', 'mon': '03', 'yr': '2004'}
Question page & discussion
Question 6 Medium

What is the output of the following Python program?

main.py
import re
s = "<b>x</b><i>y</i>"
print(re.findall(r"<.*>", s), re.findall(r"<.*?>", s))
A ['<b>', '</b>', '<i>', '</i>'] ['<b>x</b><i>y</i>']
B ['<b>x</b>', '<i>y</i>'] ['<b>', '<i>']
C ['<b>x</b><i>y</i>'] ['<b>', '</b>', '<i>', '</i>']
D ['b', 'i'] ['b', 'i']
Question page & discussion
Question 7 Easy

What is the output of the following Python program?

main.py
import re
print(repr(re.sub(r"\s+", " ", "  a   b \t c ")), re.sub(r"a", "X", "banana", count=2))
A 'a b c' bXnXnX
B ' a b c ' bXnXna
C ' a b c ' bXnXnX
D 'a b c' bXnXna
Question page & discussion
Question 8 Medium

What is the output of the following Python program?

main.py
import re
print(re.sub(r"(\d{4})-(\d{2})-(\d{2})", r"\3/\2/\1", "From 2026-11-30 to 2026-12-05"))
A From 2026/11/30 to 2026/12/05
B From \3/\2/\1 to \3/\2/\1
C From 30/11/2026 to 2026-12-05
D From 30/11/2026 to 05/12/2026
Question page & discussion
Question 9 Medium

What is the output of the following Python program?

main.py
import re
print(re.split(r"[,;]\s*", "a, b;c ;d"), re.split(r"(-)", "1-2-3"))
A ['a', 'b', 'c', 'd'] ['1', '2', '3']
B ['a', 'b', 'c ', 'd'] ['1', '2', '3']
C ['a,', 'b;', 'c ;', 'd'] ['1', '-', '2', '-', '3']
D ['a', 'b', 'c ', 'd'] ['1', '-', '2', '-', '3']
Question page & discussion
Question 10 Medium

What is the output of the following Python program?

main.py
import re
pairs = re.findall(r"(\w+)=(\d+)", "x=1 y=22 bad=z")
print(pairs, dict(pairs))
A [('x', '1'), ('y', '22')] {'x': '1', 'y': '22'}
B ['x=1', 'y=22'] {'x': '1', 'y': '22'}
C [('x', '1'), ('y', '22'), ('bad', 'z')] {'x': '1', 'y': '22', 'bad': 'z'}
D [('x', 1), ('y', 22)] {'x': 1, 'y': 22}
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links