Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Python equivalent for PHP's implode?

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*

Draft saved
Draft discarded
Cancel
2
  • 1
    join() works great if you have an array of strings, but if any member of the array is int instead of a string, you'll get a TypeError, php's implode doesn't do that, even in strict mode =/ <?php declare(strict_types=1);var_dump(implode("glue",["startString",(int)123,"endString"])); gives you string(31) "startStringglue123glueendString" but in python doing "glue".join(["startString",123,"endString"]); gives you TypeError: sequence item 1: expected str instance, int found Commented Jun 1, 2020 at 18:11
  • 1
    @hanshenrik fix is "_".join(map(str, tuple)) or trough comprehension "_".join([str(x) for x in tuple]) Commented Feb 20, 2024 at 8:49

default

AltStyle によって変換されたページ (->オリジナル) /