-1
for size in [1, 2, 3, 4]:
    result = 0
    print("size=" + str(size))
    for element in range(size):
        result = result + element
        print(" adding " + str(element)+", result so far=" + str(result))
    print("Done. size=" + str(size) + " result=" + str(result))
print("All done!")

I wonder why we use the str() function on size and result in the 7th line. I know we can't use integer and string in a single print function, but if the rule is like this, why isn't there any problem in this code (about integer + string rule)?

x=18
print("Hello , I am" , x , "years old.")

We use integer and string in a single line don't we ?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Gingerbread
  • 53
  • 1
  • 4
  • 9
  • 3
    In the first code, you print a string. In the second code you print a tuple. – Chr Oct 21 '16 at 07:58
  • 1
    @C.Dlp - That's only in Python 2, and it looks like the OP is using Python 3, in which case it's a function call with multiple arguments rather than a statement working with a single tuple. – TigerhawkT3 Oct 21 '16 at 08:06

3 Answers3

1

It's about the order things are run:

"Done. size=" + str(size) + " result=" + str(result)

has to be done first, before the print function is called. The print function just gets the single joined string - it has no idea that a + was used to construct it.

In this case:

print("Hello , I am" , x , "years old.")

the print function gets all 4 arguments (actually, a 4 element tuple) and can now work its magic on converting each one to a string itself (see Unnecessary detail below).

By the way, if you object to having to use str() then there are other ways of formatting the string to print. The following lines all produce the same output:

print("Done. size=" + str(size) + " result=" + str(result))
print("Done. size=%d result=%d" % (size, result))
print("Done. size={} result={}".format(size, result))
print(f"Done. size={size:} result={result:}")

The last one f" " requires Python 3.6 or later.

Unnecessary detail:

The conversion to str() in the print() function is actually done (in the C implementation) by the python API call PyFile_WriteObject() using the flag Py_PRINT_RAW. If Py_PRINT_RAW is given, the str() of the object is written.

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • print("Done. size=" ,size," result=",result) It also worked.Can we concatenate str and int objects with comma ? – Gingerbread Oct 21 '16 at 08:17
  • It is not concatenating objects with the comma. Print just receives a few arguments, transform each of them to strings and later concatenates them. – Juan Leni Oct 21 '16 at 08:26
  • @Gingerbread: no. A comma is not an operator in python, and it would be hellishly confusing if it was. The `print` function reads each argument and performs a `str()` on it itself. What's wrong with the mechanisms I showed you? – cdarke Oct 21 '16 at 09:24
1

Just because two references appear together in a single line doesn't mean they're being concatenated. If the first example, you are concatenating strings (which is why they all have to be strings) and sending them to the print function. In the second example, you are sending multiple arguments to the print function, which will print each one. The print function can handle an arbitrary number of arguments, which don't necessarily have to be of the same type.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

The plus (+) operator is not able to add strings and numbers. That's why you need to use str().

If you try:

"hello" + 2

you will get the error:

Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

When using print(a, "b", c, d) you are passing multiple arguments and there is not need for concatenation. Each argument is transformed to a string before printing.

Juan Leni
  • 6,982
  • 5
  • 55
  • 87