📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    Complete the Python code to check if a string email

    matches a simple email pattern (e.g., [email protected]). import re def is_valid_email(email):     pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"     _________ # Line to complete
    A return re.match(pattern, email) Correct Answer Incorrect Answer
    B return re.search(pattern, email) Correct Answer Incorrect Answer
    C return re.fullmatch(pattern, email) Correct Answer Incorrect Answer
    D return re.findall(pattern, email) Correct Answer Incorrect Answer
    E return re.compile(pattern).match(email) Correct Answer Incorrect Answer

    Solution

    Correct Answer: C (re.fullmatch ensures the *entire* string matches the pattern, which is typically desired for validation like email. re.match only checks from the beginning, and re.search checks anywhere in the string.)

    Practice Next
    ask-question