Is there an equivalent for PHP’s implode in Python? I’ve read in and split up a set of delimited words, and now I want to sort them out in random orders and print the words out with spaces in between.
implode — Join array elements with a string
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use the strings join-method.
You can join any iterable (not only the
listused here) and of course you can use any string (not only' ') as the delimiter.If you want a random order like you said in your question use shuffle.
In the comment there was the question why Python throws an error if you do
"glue".join(["startString", 123, "endString"]).joinoperates on an iterable of strings. There is no implicit type conversion in Python.But of course there is a solution. Just do the conversion yourself.
"glue".join(map(str, ["startString",123,"endString"]))