If you do a join() after a split() using the same delimiter, does this result in the original string?

Question

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
10 Likes

A lesser known feature of .join() is its ability to work with strings, too.

>>> "*".join('mississippi')
'm*i*s*s*i*s*s*i*p*p*i'
>>> 
>>> ' '.join([chr(x) for x in range(65, 91)])
'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'
>>>
48 Likes

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.

1 Like

We can do:

letters = ["a", "b", "c", "d"]

word = "abcd"

print("".join(letters)) #==> abcd

print(word.split("")) #==> error

Is this the only exception, when the separator is an empty string?
For this particular case it seems list(string) does the job

Given the value error is very specific:

ValueError: empty separator

I think this is a safe assumption.

2 Likes

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.

reapers_line_one_words = ["Black", "reapers", "with", "the", "sound", "of", "steel", "on", "stones"]

reapers_line_one_reverse = ' '.join(reapers_line_one_words).split()

print(reapers_line_one_reverse)

Output: [‘Black’, ‘reapers’, ‘with’, ‘the’, ‘sound’, ‘of’, ‘steel’, ‘on’, ‘stones’]

3 Likes

Nope, that’s fine. Adding the empty string '' to the end of a string is fine and is needed so that join knows to put the last delimiter in place.

>>> 'fred'.split('d')
['fre', '']
>>> 'd'.join('fred'.split('d'))
'fred'
>>> # this is the same as
>>> 'fre' + 'd' + ''
'fred'
>>>

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'
>>> 

https://docs.python.org/3/library/stdtypes.html#str.split