I wrote this code in python:
def master_yoda(text):
a=text.split()
a=a[::-1]
a=" ".join(a)
return a
Which gives the output 'home am I'
executed with master_yoda('I am home')
.
Whereas if I put an empty space in between — a=''.join()a
—
the output becomes 'homeamI'
.
Why does it happen?
The string that invokes the join
method is used to join the elements, i.e., it appears between each element of the list in the result.
>>> "-".join(["a", "b", "c"])
'a-b-c'
>>> "1".join(["a", "b", "c"])
'a1b1c'
>>> "hi".join(["a", "b", "c"])
'ahibhic'
The empty string is also a string; it just has 0 characters in it. Therefore, in the result, there are zero characters between each element of the list.
>>> "".join(["a", "b", "c"])
'abc'