1

Lets say I have string: s = '{1} goes to {0}'

And I want to format this string with list: l = ['Hollywood', 'Frankie']

I cannot modify string and list both. Is there way to write simple piece of code to handle this case?

PS. I know about question "Python Format String with List", but it is not what Im asking.

1 Answer 1

1

Use the unpack operator * when passing the list to the format method.

s = '{1} goes to {0}'
l = ['Hollywood', 'Frankie']
print(s.format(*l))

This outputs:

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

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.