Skip to content

The Simpsons Solution


import re

newquote = re.sub(pattern="\s+", repl=" ", string=quote)

print(newquote)
# Me Fail English? That's Unpossible.

Explanation

  1. \s matches a single whitespace character using the character set [ \t\n\r\f\v].

    Me Fail   English?\nThat's\tUnpossible.  <- match 1
    Me Fail   English?\nThat's\tUnpossible.  <- match 2
    Me Fail   English?\nThat's\tUnpossible.  <- match 3
    Me Fail   English?\nThat's\tUnpossible.  <- match 4
    Me Fail   English?\nThat's\tUnpossible.  <- match 5
    
  2. \s+ matches one or more whitespace characters.

    Me Fail   English?\nThat's\tUnpossible.  <- match 1
    Me Fail   English?\nThat's\tUnpossible.  <- match 2
    Me Fail   English?\nThat's\tUnpossible.  <- match 3
    
  3. re.sub(pattern="\s+", repl=" ", string=quote) replaces each match with a single space.

    Me Fail English? That's Unpossible.
    

See the problem