Reputation: 543
t=('1','2','3')
print '-'.join(t).join(t)
The output of above code is
11-2-321-2-33
How?
Upvotes: 1
Views: 319
Reputation: 103
What's actually happening is that Python is evaluating your expression by first solving the inner "join" - which outputs "1-2-3", then using that string to join your tuple. Let's call that first string "-AaA-" - what you see in the end is basically: 1-AaA-2-AaA-3. only instead of my string, you get the tuple joined by "-", surrounded by the tuple.
it's the same as: '1-2-3'.join(t)
Upvotes: 0
Reputation: 848
Okay, so let's break down "print '-'.join(t).join(t)" into two separate method calls. Because functionally, this is what happens in the program.
First call: '-'.join('1','2','3') will return '1' + '-' + '2' + '-' + '3' or in short '1-2-3'. I substituted 't' with its actual values in the program. The result of this method call will then be used as input for the second method call. Which brings us to..
Second call: '1-2-3'.join('1','2','3') will return '1' + '1-2-3' + '2' + '1-2-3' + '3' or '11-2-321-2-33'
Hope this clarifies this for you
Upvotes: 2