# Make str.join convert non-str objects implicitly

**URL:** <https://discuss.python.org/t/make-str-join-convert-non-str-objects-implicitly/41428>\
**Category:** Ideas\
**Created:** [December 19, 2023, 2:46am UTC](https://discuss.python.org/t/make-str-join-convert-non-str-objects-implicitly/41428 "2023-12-19T02:46:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![blhsing](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/blhsing/32/25812_2.png) [@blhsing](https://discuss.python.org/u/blhsing)\
**Post date:** [December 19, 2023, 2:46am UTC](https://discuss.python.org/t/make-str-join-convert-non-str-objects-implicitly/41428/1 "2023-12-19T02:46:03Z")

</div>

While I understand the mantra of “Explicit is better than implicit.” in most cases, I wonder what could possibly be a downside to making `str` conversions implicit for the `str.join` method.

Wouldn’t this be more intuitive and readable:

```auto
' '.join([1, 2, 3])

```

than the currently accepted boilerplate of mapping items to `str` first before joining them into a string:

```auto
' '.join(map(str, [1, 2, 3]))

```

I mean, the [current code](https://github.com/python/cpython/blob/893c9ccf48eacb02fa6ae93632f2d0cb6778dbb6/Objects/unicodeobject.c#L9653) already spends the CPU time to validate that each item is a string, so why not do the helpful thing of converting the item to a string instead of complaining about it, since the intention is always to join the items as a string?

---

<div class="post-metadata">

**Author:** ![bschubert](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bschubert/32/26920_2.png) [@bschubert](https://discuss.python.org/u/bschubert)\
**Post date:** [December 19, 2023, 3:00am UTC](https://discuss.python.org/t/make-str-join-convert-non-str-objects-implicitly/41428/2 "2023-12-19T03:00:23Z")

</div>

Should `str.join` use an object’s ` __str__ ` or its ` __repr__ `?

There’s no definite answer for what the would user want. It’s better to let them explicitly decide how they want their objects to be stringified.

Another part of the Zen comes to mind:

> In the face of ambiguity, refuse the temptation to guess.

---

<div class="post-metadata">

**Author:** ![blhsing](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/blhsing/32/25812_2.png) [@blhsing](https://discuss.python.org/u/blhsing)\
**Post date:** [December 19, 2023, 3:06am UTC](https://discuss.python.org/t/make-str-join-convert-non-str-objects-implicitly/41428/4 "2023-12-19T03:06:33Z")

</div>

Then why does `print` call `str` implicitly for non-`str` objects, relying on the implicit behavior of `str` calling ` __repr__ ` if ` __str__ ` doesn’t exist?

As long as the rules are documented and reasonably intuitive, I don’t see why not.

---

<div class="post-metadata">

**Author:** ![dtrodrigues](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dtrodrigues/32/10441_2.png) [@dtrodrigues](https://discuss.python.org/u/dtrodrigues)\
**Post date:** [December 19, 2023, 3:09am UTC](https://discuss.python.org/t/make-str-join-convert-non-str-objects-implicitly/41428/5 "2023-12-19T03:09:15Z")

</div>

Previous discussions:

> [@Why not make str.join() coerce the items in its iterables](https://discuss.python.org/t/why-not-make-str-join-coerce-the-items-in-its-iterables/24097):
>
> Definitely, one of the most boring and repetitive patterns I find myself typing is ", ".join(str(i) for i in mything). If str.join would call \_\_str\_\_ in each element in the iterator it is given, this kind of code could be simply ", ".join(mything). I don’t see this change breaking anything, and I see a lot of real-world code getting rid of a pain to get a pretty output. Also, I can’t see this having any compatibility issues, since, up to know everyone has to painstakingly iterate over their…

> [@Allow list of integers in \`str.join\`](https://discuss.python.org/t/allow-list-of-integers-in-str-join/23676):
>
> I wondered today for the Nth time why the following is not permitted - the intent seems clear enough: \>\>\> import sys \>\>\> '.'.join(sys.version\_info[:2]) Traceback (most recent call last): File "\<stdin\>", line 1, in \<module\> TypeError: sequence item 0: expected str instance, int found Of course, it’s possible to work around this \>\>\> '.'.join([str(x) for x in sys.version\_info[:2]]) '3.11' but it seems… unnecessary? Once I’m calling from a string.join method, what possible other output than a…

---

<div class="post-metadata">

**Author:** ![davidism](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/davidism/32/3295_2.png) [@davidism](https://discuss.python.org/u/davidism)\
**Post date:** [December 19, 2023, 3:53am UTC](https://discuss.python.org/t/make-str-join-convert-non-str-objects-implicitly/41428/6 "2023-12-19T03:53:33Z")

</div>

Closing as this has already been thoroughly discussed.
