Short function to remove unnecessary whitespace I have a function consisting of one line of code:

def trimString(string):
    """ Remove unnecessary whitespace. """
    return re.sub('\s+', ' ', string).strip()


But I've been debating with myself whether the following would be better, seeing as explicit > implicit.

def trimString(string):
    """ Remove unnecessary whitespace. """
    string = re.sub('\s+', ' ', string).strip()
    return string


So which is preferable, the former or the latter? And why is that the case?

The question may be off-topic but I find myself asking it often enough and thought this was the place to ask.
I think the shorter, the better. Since you are in Python 3, if you really want to make explicit that the function is returning a string, you can use type hints:

def trimString(string) -&gt; str:


You can also specify it in the parameter:

def trimString(string: str) -&gt; str:


(keep in mind that Python will ignore type hints, but some IDEs like PyCharm use it to detect warnings and errors)

On a side note, you should try to follow the PEP 8 styling conventions; the function name should be in camel case, so trim_string.