It is well documented in numerous that str
is required to convert ints to strings before they can be concatenated:
'I am ' + str(n) + ' years old.'
There must be a fundamental reason why Python does not allow
'I am ' + n + ' years old.'
and I would like to learn what that reason is. In my project, I print a lot of numbers and end up with code like this.
'The GCD of the numbers ' + str(a) + ', ' + str(b) + ' and ' + str(c) + ' is ' + str(ans) + '.'
It would be much prettier if I could drop 'str'. And here's what makes my experience particularly vexing. I'm working with SymPy, so I have code like this:
'Consider the polynomial ' + (a*x**2 + b*x + c)
This works 99% percent of the time because we overloaded the '+' for SymPy expressions and strings. But we can't do it for ints and, as a result, this code fails when a=b=0 and the polynomial reduces to an integer constant! So for that outlier case, I'm forced to write:
'Consider the polynomial ' + str(a*x**2 + b*x + c)
So again, the solution 'string' + str(int) is straightforward, but the purpose of my post is to understand by Python doesn't allow 'string' + int the way, for instance, Java does.