When printing the final product [print(“The total price is”, total_price)], there is a space between “is” and the numeric value of total_price. Why is this?
First of all, welcome to the community!
As for your query, “,” acts as a separator between two parameters by giving a “space”.
a = "apple" b = "mango" print(a, b)
will give the following output:
It also saves time, but string concatenation using “+” gives you more control for output formatting.
If you want to use “+” operator, you have to write as:
total_price = 0 new_sneakers = 50.00 total_price += new_sneakers nice_sweater = 39.00 fun_books = 20.00 # Update total_price here: total_price += nice_sweater + fun_books print("The total price is " + str(total_price))
NOTE: You cannot simply write
print("The total price is " + str(total_price)), as “+” operator only concatenate strings.
I want to clarify, that the reason there is a space is that print() has a few arguments, but only the first is required. For our purposes
print(*objects, sep=' ') is what’s relevant. Which is any number of object arguments, and a keyword argument for the separator. If no separator is defined, the default value is a space, which will be placed in between each object as is done here.
You can absolutely write
print("The total price is " + str(total_price)) as the
str() function happens before concatenation so you adhere to the rule of only concatenating strings.