-1

This works,

In [2]:
"I said " + ("Hey " * 2) + "Hey!"
Out[2]:
'I said Hey Hey Hey!'

but this doesn't. Why?

In [3]:
"The correct answer to this multiple choice exercise is answer number " + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    "The correct answer to this multiple choice exercise is answer number " + 2
TypeError: must be str, not int
martineau
  • 119,623
  • 25
  • 170
  • 301
George Katsanos
  • 13,524
  • 16
  • 62
  • 98
  • 1
    `"Hey " * 2` has a clear meaning, and that meaning is clear _even if one doesn't ever do any automatic conversion or casting of the integer to a non-integer type_. What is `'Hey ' + 2` supposed to mean? The only way I can make it make sense is if one introduces automatic type conversion to make it into a string; choosing not to do that is a very sensible design decision, and consistent with other design thinking around Python as a whole: [Explicit is better than implicit, and in the face of ambiguity, one should avoid the temptation to guess](https://www.python.org/dev/peps/pep-0020/). – Charles Duffy Dec 10 '20 at 16:11
  • What was your intention with the + 2? Maybe you were looking for a format string to put that integer in the output? – FloWil Dec 10 '20 at 16:12
  • One means multiplying the STRING content, the other is concatenation for that you need string element, not ints – azro Dec 10 '20 at 16:12
  • Does this answer your question? [How to resolve TypeError: can only concatenate str (not "int") to str](https://stackoverflow.com/questions/51252580/how-to-resolve-typeerror-can-only-concatenate-str-not-int-to-str) with its 3 duplicates inside – Tomerikoo Dec 10 '20 at 16:12
  • 1
    [That said, "why" questions about language design are generally considered not to be answerable here.](https://meta.stackexchange.com/questions/170394/what-is-the-rationale-for-closing-why-questions-on-a-language-design); our focus is on _practical_ questions, and if you know how to avoid the exception, you've already solved your practical problem. – Charles Duffy Dec 10 '20 at 16:15
  • I guess the main reason behind my question is that I'm a JS developer and in JS that's perfectly normal (and even convenient) – George Katsanos Dec 10 '20 at 20:08
  • @Charles Duffy I understand that's not strictly a SO question, in this case where would I post a question that points to understand the reasoning behind this design? I think my question wasn't formulated correctly. – George Katsanos Dec 13 '20 at 11:51
  • Reddit, maybe? There's a whole wide world outside the Stack Exchange network. – Charles Duffy Dec 13 '20 at 15:16

1 Answers1

2

It's okay to multiply a string by an integer -- the result is N repetitions of the string.

I's not okay to add a string to an integer. The people who designed Python decided that this is not allowed.

That's just how Python works.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • 2
    *"The people who designed Python decided that this is not allowed"* - quite rightfully must add. Seems like the logical thing to do. What would the result of `"5" + 5` be? The interpreter can't know if you want it to be `"55"` or `10`... – Tomerikoo Dec 10 '20 at 16:14
  • @Tomerikoo sorry, I tend to disagree about the logical thing. If you expect mathimatical operators to only work with numbers, you should do this with all of the operators and not some of them. It's obvious that 5+5 = 10 , "5" + 5 = "55" by implicitly typecasting to a string – George Katsanos Dec 10 '20 at 20:11
  • 1
    @GeorgeKatsanos how is that so obvious? Why not implicitly typecast to an int? It is very common to have string representation of numbers and maybe I want to directly sum them as ints. I think the bottom line is as you can see it's subjective and has opinions to both ways and I believe this is why the designers simply chose to go with `explicit over implicit` kind of attitude and leave the implicit conversions to the real obvious, non-ambigious cases... – Tomerikoo Dec 13 '20 at 07:50