Print string + int , syntax?


#1

Hi the community,

Working on an exercise about lists in Computer Science path. I had to print this :

print(“we sell”+ str( num_pizzas)+ " different kind of pizza!")

But I don’t know why, i decided to write that:

print(“we sell”, num_pizzas, “different kind of pizza!”)

I was surprised to see that it worked fine, no need to convert int in str. Why? Do you have an idea?


#2

Hi @maetis31,

Python allows us to pass multiple arguments to the print function, and representations of their values get displayed in order, with spaces in between. However, it is best to formulate the format of the output explicitly.

This is even better than concatenating together the output …

print("we sell {:d} different kind of pizza!".format(num_pizzas))

More recent versions of Python offer additional formatting techniques.