The use of \s in replace vs regular expressions
Question: While learning python I got my first real python stumper when processing a
multi line file. It seems like `\s` in the replace method does not remove
newlines, where `\s` remove newlines when used in a regular expressions. I can
remove the newlines using replace `\n` just fine, but I am troubled that the
definition of `\s` is different for the replace method and regular
expressions. Is this really the case?

    
    
    s_clean = s.replace('\s', '')
    
    import re
    s_clean = re.sub(r'\s', '', s)
    


Answer: Yes `string.replace` is different from `re.sub`. The former replaces the
substrings you ask to replace and the latter replaces substrings which are
occurrences of the _pattern_ you give it.

This design is intentional, and probably not something that should trouble
you. After all, one is a method on strings, the other is a method on regexes.

Here's another example that you might find interesting:

    
    
    >>> import re
    >>> s = "aaaa+b"
    >>> s.replace("a+", "c")
    'aaacb'
    >>> re.sub(s, "a+", "c")
    'c'
    

I think this is pretty sensible. Yes it is true that other languages expect
either a regex or a string in `replace`, but Python actually keeps them
distinct!

