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


    Question

    What does re.sub(r'(\w+)', r'_\1_', 'hello world')

    return?
    A _hello_ _world_ Correct Answer Incorrect Answer
    B _hello_world_ Correct Answer Incorrect Answer
    C hello world Correct Answer Incorrect Answer
    D _hello_ _world Correct Answer Incorrect Answer

    Solution

    The re.sub() function replaces occurrences of a pattern. Here, (\w+) captures each word. In the replacement string r'_\1_', \1 refers to the content of the first (and only) capturing group. Thus, each word is wrapped with underscores.

    Practice Next
    ask-question