Interesting Python Split Behavior
Had a recent challenge that didn’t hold me up for long. I tried what was the correct solution on a whim and it happened to work, but I wanted to analyze the behavior. The goal was to build a function that accepts a string and modifies it according to the examples:
"This is an example!" ==> "sihT si na !elpmaxe"
"double spaces" ==> "elbuod secaps"
The characters in each individual word are reversed, the ordering of the words are preserved as are the number of spaces between words.
Here was my ultimate solution:
myInput = input("string plz: ")
def reverse_words(text):
return(" ".join([word[::-1] for word in text.split(" ")]))
print(reverse_words(myInput))
It was that text.split(" ")
bit that I didn’t quite understand. Because my initial understanding of split()
was that its default delimiter was a simple whitespace. However let’s review the official documentation:
If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.
So if you don’t specify, it will group whitespaces together as if they were a single whitespace. If you specify a whitespace then it keeps them separate. We can see that behavior here:
>>> myString = "double spaces"
>>> myString.split()
['double', 'spaces'] # only 2 items
>>> myString.split(" ")
['double', '', 'spaces'] # 3 items
>>> " ".join(myString.split())
'double spaces' # there's only a single space
>>> " ".join(myString.split(" "))
'double spaces' # we got back all of the spaces we started with
When I split()
it simply ignores the grouped-together whitespaces. Though when I split(" ")
then it refrains from grouping them together as a single delimiter, preserving that extra “word” in between the two whitespaces and allowing me to rejoin the words together and preserve that extra space. That’s why it works. Cool.