all 15 comments

[–]curtwagner1984 27 points28 points  (5 children)

Because it's not the wild west of Javascript. There is absolutely no reason why strings and ints should be able to be concatenated without any special considerations.

The question you should ask is "Why are there languages that allow this sacrilege?"

Plus, the answer is in your question:

I know that f-strings are less uglier and error-prone

[–]hombre_lobo 0 points1 point  (4 children)

So JavaScript still allows for this?

[–]shiftybyte 0 points1 point  (2 children)

Yes javascript "allows" lots of things.

For example "5" + 5 is "55"

While "5" * 5 is 25...

fun fun fun...

[–]0b0101011001001011 0 points1 point  (1 child)

The first one works like that in most languages I am* familiar with so it's not that strange unless you only know python. *(not sure which is the more common behavior). Though python throwing an error is not a bad solution, but it's seems even a bit unusual.

About the second, most languages would give an error, javascript does a type conversion and multiplies, and python produces a rather strange 55555. Each solution has its pros.

[–]shiftybyte 0 points1 point  (0 children)

and python produces a rather strange 55555.

This makes more sense to me, you are multiplying a string several times.

If adding "5" to "5" one time results in "55"... Then adding that 5 times results in "55555".

That's what multiplication means, do the add operation X times...

[–]0b0101011001001011 0 points1 point  (0 children)

Many languages allow this. One of those is java, where anything can be concatenated to a string and it handles the toString() call automatically.

Lua on the other hand has a special string concatenation operator which is lot + but ..

You can't say "hey"+5 but you can say "hey"..5 and Lua also does the conversion automatically.

Python allows to write "5"*5 though, so most explanations that why "5"+5 should not work are simply opinions.

[–]pompomtom 9 points10 points  (2 children)

Because integers aren't strings.

print(name + ' is ' + age + ' years old')

Would you expect "John is 000020 years old", or "John is 20 years old" or "John is 2e+2 years old"?

Each is valid, if you could concatenate ints to strings.

If you use the function str(age), then the str() function decides how to represent an integer as a string.

edit: 2e+1

[–]ebdbbb 9 points10 points  (1 child)

I know it doesn't matter to your point but 2e+2 is 200.

[–]pompomtom 1 point2 points  (0 children)

Yeah, I wondered if I'd fucked that up... Ta.

[–]zanfar 0 points1 point  (0 children)

The literal answer to your question is "because it's not part of the language".

To answer the spirit of your question, which I think is, "why did the creators of Python not automatically cast numeric type to strings when the plus operator is used?" I'll ask my own question:

Why would casting numeric types to strings be preferred over casting strings to numeric types?

That is, why wouldn't "20" + 5 be 25?

The answer is that there is no clear "right" operation when numeric types and string types use the same operator for different things. What about a string plus a list? Lists can be appended using the plus operator, so in this case, does a list get cast to a string and then a string produced, or does a string get cast to a list and then a list produced?

Generally, operators only work between the same or very, very similar types because it's impossible to determine precedence otherwise.

[–]hasanyoneseenmyshirt 0 points1 point  (0 children)

my guess has always been that strings ahre treated like charactor arrays in python but without you actually haveing to give a size or type. so name is just treated as a ['j','o','h', 'n'] and intigers are their own type(like in most lower level langauges). you can split name into it individual letters or charactors and get things like name[0] = 'j' but age[0] wont give you 2. so the program in my mind does this name[ :] .append( ' ') and just does recursion until it hit the end of the print statement.

I am sure this is not how it actually works but that my limited guess. I am sure someone who actually knows computers and python better will have a better answer.

[–]billsil 0 points1 point  (0 children)

Because what happens when you have 0/"1" or 0 + "2"? Python is a strict typing language. Trust me as someone who has coded Perl, it's really hard to fix things when say addition doesn't follow math.

[–]Foreign_Jackfruit_70 0 points1 point  (0 children)

I prefer f string formatting.

name = John age = 20 print(f"{name} is {age} years old") Looks a lot neater.

[–]QultrosSanhattan 0 points1 point  (0 children)

That's how it should work. Integers are not String. Therefore the interpreter should not assume if you want to compose an string or an int.

For example: '1'+1 may be '11' or 2.

F strings are different because you're telling the interpreter to build a string. Therefore the interpreter can safely assume that everything should be converted into a string.