2

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.

Wynne
  • 210
  • 2
  • 9
  • 4
    Because explicit is better than implicit; concatenation requires explicit conversion, objects are not normally coerced implicitly. – Martijn Pieters May 13 '17 at 18:16
  • You should really learn about `str.format()`: `'The GCD of the numbers {}, {} and {} is {}.'.format(a, b, c, ans)`. Python 3.6 and up offer `f` strings: `f'The GCD of the numbers {a}, {b} and {c} is {ans}.'` – Martijn Pieters May 13 '17 at 18:17
  • Some explanations could be found here: https://softwareengineering.stackexchange.com/q/304445/202154 – maxkoryukov May 13 '17 at 18:26
  • @MartijnPieters Strange, but you can do the same with print("I am ", n, " years old.") and nobody complains about implicit conversion for some reason. So replacing "+" operator with comma suddenly makes it ok ? – bsk Aug 16 '21 at 10:48
  • @BulgarSadykov `print()` **explicitly** converts all arguments to strings using `str()`, [as documented](https://docs.python.org/3/library/functions.html#print): *All non-keyword arguments are converted to strings like `str()` does*. `print()` has an explicit, narrow use case, the `+` operator has a far broader use case that goes way beyond strings. – Martijn Pieters Aug 16 '21 at 13:02

2 Answers2

3

you can use "," instead of "+" :

a=4
x=5
b=6
c=5
n=100
my_age="I am",n,"Years old"
print("I may say",my_age)

print('Consider the polynomial ' ,(a*x**2 + b*x + c))
legend-is-back
  • 443
  • 2
  • 12
1

It has to do with the + operator.

When using the + operator, depending on the a and b variable types used in conjuncture with the operator, two functions may be called:

operator.concat(a, b) or operator.add(a, b)

The .concat(a, b) method returns a + b for sequences.

The .add(a, b) method returns a + b for numbers.

This can be found in the pydocs here.

Python doesn't implicitly convert variable types so that the function "works". That's too confusing for the code. So you must meet the requirements of the + operator in order to use it.

pstatix
  • 3,611
  • 4
  • 18
  • 40
  • 1
    So is there a reason why the designers of Java decided that this would be a good idea and the designers of Python decided that this would be a bad idea? What's the fundamental design difference? – Wynne May 14 '17 at 18:21
  • @Lem.ma I'm not really sure why, but I'm positive it has to do with how the two languages chose to handle type coercion. I recommend you read about String Formatting for some cases in lieu of concatenation. You can read more about it [here](https://docs.python.org/2/library/stdtypes.html#string-formatting). – pstatix May 14 '17 at 19:03