1
print ("hello"+"world)

or

print ("hello"
+"world")

would give the output as:

hello world

however, when I try to insert a variable (int) into the print functions along with 'str' using '+', there's an error. But, when I use ','s instead, it is designed to work. That is:

x=1
print("the value of 'x' is " + x + ".")

it shows the error

TypeError: can only concatenate str (not "int") to str

But when I use:

print("the value of 'x' is ",x,".")

I get the desired output.

I would like to know why is python designed this way, what is the meaning of using '+' in this context. Thankyou.

Edit: Thanks for the replies, the intent was because in Java + means to simply put together.

sighclone
  • 102
  • 1
  • 10
  • 3
    `+` is an operator. It works on strings with other strings to do concatention, and with numeric types and other numeric types for *addition*. The operator is not defined between `str` and numeric types. – juanpa.arrivillaga Oct 02 '20 at 04:17
  • 3
    This only works with the commas because `print()` is a function that can accept arguments of different types. You are passing multiple arguments to it and the arguments are separated by commas. The `print()` function then produces reasonable output based on the type of the argument. If you tried to do `new_string = "the value of 'x' is ", x, "."` and then `print(new_string)` you will get a very different result. – Craig Oct 02 '20 at 04:35
  • 2
    What do you consider a viable answer to “why?”? A quote from the implementer? A technical argument? Historical comparison to other languages? – MisterMiyagi Oct 02 '20 at 05:44
  • If you try to “+” a string and a number, you could mean to add two numbers but have mistyped one of them, or you could mean to concatenate two strings but mistyped one of them, or you could mean to concatenate two lists and mistyped both. It’s not unambiguously clear what exactly you want and whether it might be a mistake. Python *could* try to coerce the types according to some rules, like other languages do. But that can fail as often as it succeeds in subtle ways. So Python is requiring you to be *explicit* about it. – deceze Oct 02 '20 at 05:56

2 Answers2

3

Treating this operation as “+ in print function” is the wrong mental model. There are two entirely separate operations here:

  • print which knows it needs to derive a string to display it,
  • + which knows nothing about the followup usage of its result.

Functions (and by extension methods and operators) in Python are not return type polymorphic, which is a fancy way of saying functions do not know what output type is expected from them. Just because + is used to derive the argument to print does not tell it that a string is the desired result.

The operation :str + :int has no well defined output type. What is "1" + 1? Is it 2, is it "11", is it 11 or something else? There is no clear answer for these two types and Python thus consistently raises TypeError. Whether or not the result is eventually used in print is irrelevant.

In contrast, print serves to write a string-representation of its arguments to a stream. Every argument must eventually result in a string and be written to the stream. The operation print(:str, :int) does not need to concatenate a str and int - it can separately convert each to str and write it immediately.

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
2

print() is just a function and it was designed in a way that it accepts variable number of positional arguments and it internally convert them to str.

As you can see here at it's documentation:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False):

. . .

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.
. . .

When you do print("the value of 'x' is ",x,".") you are passing 3 distinct arguments to the function which the function gets as objects. And they are converted to string as mentioned above.

However, in the line print("the value of 'x' is " + x + ".") you're trying to concatenate the values and pass as a SINGLE argument and since the concatenation operation isn't a valid operation (python doesn't have coercion like javascript does to convert unlike types amid operations), it fails before it ever reaches the print function.

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36