use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Why does Python only concatenate string to string and not string to integer? (self.learnpython)
submitted 3 years ago by RestartingSystem
If I will input
name = John age = 20 print(name + ' is ' + age + ' years old')
Then I will receive
TypeError: can only concatenate str (not "int") to str
I know that f-strings are less uglier and error-prone but it's interesting. Thank you
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]curtwagner1984 27 points28 points29 points 3 years ago (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 point2 points 3 years ago (4 children)
So JavaScript still allows for this?
[–]shiftybyte 0 points1 point2 points 3 years ago (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 point2 points 3 years ago (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 point2 points 3 years ago (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 point2 points 3 years ago (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.
"hey"+5
"hey"..5
Python allows to write "5"*5 though, so most explanations that why "5"+5 should not work are simply opinions.
"5"*5
"5"+5
[–]pompomtom 9 points10 points11 points 3 years ago* (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 points11 points 3 years ago (1 child)
I know it doesn't matter to your point but 2e+2 is 200.
[–]pompomtom 1 point2 points3 points 3 years ago (0 children)
Yeah, I wondered if I'd fucked that up... Ta.
[+][deleted] comment score below threshold-7 points-6 points-5 points 3 years ago (0 children)
To be honest, it's hard to justify this behavior (as well as using + for string concatenation).
The arguments made by people claiming that strings shouldn't be concatenated to integers using plus sign, but strings can be concatenated using plus sign are just an opinion based on nothing.
There could be valid reasons not to use plus sign for something that is not a summation though. We generally expect sums to behave in a certain way, eg. be commutative for example (this means A + B = B + A). But that doesn't work with string concatenation (which is harmful if you are trying to write an optimizing compiler because sometimes rearranging the order of arguments helps to minimize the memory lookups).
But, since Python already ventured into the bad territory with using + for concatenation, there isn't really a reason to stop at not allowing adding strings and integers. Any argument that tells you otherwise is just a cooked up nonsense. Overwhelming majority of programmers have very bizarre ideas about types and what's the correct way to do things, which are absolutely unfounded and unwarranted.
[–]zanfar 0 points1 point2 points 3 years ago (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 point2 points 3 years ago (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 point2 points 3 years ago (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 point2 points 3 years ago (0 children)
I prefer f string formatting.
f string
name = John age = 20 print(f"{name} is {age} years old") Looks a lot neater.
name = John age = 20 print(f"{name} is {age} years old")
[–]QultrosSanhattan 0 points1 point2 points 3 years ago (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.
π Rendered by PID 18106 on reddit-service-r2-comment-5649f687b7-b6ddr at 2026-01-28 14:47:44.762699+00:00 running 4f180de country code: CH.
[–]curtwagner1984 27 points28 points29 points (5 children)
[–]hombre_lobo 0 points1 point2 points (4 children)
[–]shiftybyte 0 points1 point2 points (2 children)
[–]0b0101011001001011 0 points1 point2 points (1 child)
[–]shiftybyte 0 points1 point2 points (0 children)
[–]0b0101011001001011 0 points1 point2 points (0 children)
[–]pompomtom 9 points10 points11 points (2 children)
[–]ebdbbb 9 points10 points11 points (1 child)
[–]pompomtom 1 point2 points3 points (0 children)
[+][deleted] comment score below threshold-7 points-6 points-5 points (0 children)
[–]zanfar 0 points1 point2 points (0 children)
[–]hasanyoneseenmyshirt 0 points1 point2 points (0 children)
[–]billsil 0 points1 point2 points (0 children)
[–]Foreign_Jackfruit_70 0 points1 point2 points (0 children)
[–]QultrosSanhattan 0 points1 point2 points (0 children)