In the context of this exercise, if you do a join() after a split() using the same delimiter, does this result in the original string?
Answer
Yes, this should result in the original string. This is because join() and split() are essentially inverses of each other.
When we split, it returns a list of substrings that the delimiter was separating. And, when we join on the same delimiter, it merely places that delimiter back where the splits were performed, resulting the same string.
original = "*A*B*C*D"
splitted = original.split("*")
joined = "*".join(splitted)
print(joined) # *A*B*C*D
Hi! New learner here. Wouldn’t this not necessarily be always true, though, in the case of a string that was split using the delimiter that is the same as its last character?
Try that image upload again. We didn’t catch it on the original post.
A string that is split on any character or sequence of characters must reassemble on join with the same character or sequence of characters as the separator.
I paraphrased the above from somewhere. The point is, there is no delimiter in this context; and, the last character of some string is not a concern given the construct.
Yes, just contributing to the topic in a quick way of seeing that. Because as you probably know we can call multiple methods, one after the other in the same statement.
As long as you specify a delimiter it works fine. The default in split is all white space of any length is one delimiter.
>>> 'Five space first'.split()
['Five', 'space', 'first']
>>> # if you use a space you lose how many spaces existed
>>> ' '.join('Five space first'.split())
'Five space first'
>>> # if you use an empty string you remove all whitespace
>>> ''.join('Five space first'.split())
'Fivespacefirst'
>>>