Exercise 1 solution import re EMAIL = re.compile(r"[\w.+-]+@[\w-]+(?:\.[\w-]+)+") def redact_emails(text): return EMAIL.subn("[email]", text) print(redact_emails("Write to ann@example.com or bob.smith+x@mail.co.uk today.")) print(redact_emails("Nothing to hide here, only a@ and @b.")) # ('Write to [email] or [email] today.', 2) # ('Nothing to hide here, only a@ and @b.', 0) (All lines were run and printed exactly what is shown.) Explanation: subn does the replacement and returns the new text together with the number of replacements, so one call gives both answers.