Python equivalent for PHP's implode?

pythonian29033 picture pythonian29033 · Aug 21, 2012 · Viewed 74.4k times · Source

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

http://php.net/manual/en/function.implode.php

Answer

Matthias picture Matthias · Aug 21, 2012

Use the strings join-method.

print ' '.join(['word1', 'word2', 'word3'])

You can join any iterable (not only the list used 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.