1

I want to do a string formatting with a list as showed below:

(pseudocode)

down_url = [url1, url2, ..., urln]    
url = "download: %s" % down_url

Yes, the list down_url has arbitrary items, and I want to get all the items from it. The result I want is:

download: url1, url2, ..., urln

I hope you can understand what I have expressed. Thank you.

1 Answer 1

3
print("download: " + ", ".join(down_url))

Explanation:

"sep".join(iterable) joins all elements in iterable using the separator sep and returns a string. So for example:

>>> "".join(("a", "b", "c"))
'abc'
>>> ", ".join(("a", "b", "c"))
'a, b, c'

In case your iterable contains elements other than strings, you can force them to be mapped into strings before passing them to the .join() method:

>>> ", ".join(map(str, ("a", 2, "c")))
'a, 2, c'
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.