1
out = 'Hello'
print( out.join([' world']) )

When I run it, it shows

world

Isn't it supposed to print hello world?

3 Answers 3

6

No, it joins the elements of the list with the word 'Hello'. For example, if you had ['A', 'B'], it would produce 'AHelloB'. Since there is only one element in your list, there is nothing to join, so it can just return the only element in there unchanged.

What you wanted is probably something like ' '.join(['Hello', 'world']).

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

Comments

2

join() behaves slightly differently to how you expect. It takes a list of words to join. The seed word is what you put between the joins.

' '.join(['Hello', 'world'])
>> Hello world

','.join(['Hello', 'world'])
>> Hello,world

'/'.join(['name', 'location', 'age'])
>> name/location/age

'*'.join(['name'])
>> name

'hello'.join(['world'])
>> world

Comments

0

Since your array contains only one entry " world" there is nothing to join.

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.