1
t=('1','2','3')
print '-'.join(t).join(t)

The output of above code is

11-2-321-2-33

How?

1
  • 1
    Small correction: what you're describing is really a chained join, not a nested join. Commented Aug 9, 2016 at 15:46

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.