Comma or concatenation?

Hi Everyone! I’m a beginner in the Data Scientist course, and I have a question.
In the Python Syntax course (the medical insurance project 7/3), we are asked the following:

Let’s display this value in an informative way. Print out the following string in the terminal:

* *This person's insurance cost is {insurance_cost} dollars.* *

You will need to use string concatenation, including the str() function to print out the insurance_cost .

Answer:
Required notation:
print(“This person’s insurance cost is” + " " + str(new_insurance_cost) + " " + “dollars.”)

My notation:
print(“This person’s insurance cost is”, new_insurance_cost, “dollars.”)

My question is: why do we have to use concatenation rather than commas? I see that both options print the same result, but the komma notation is easier since it does not require to denote space (" ") and does not require to mark the integer as a string (st(insurace cost)).

Thank you in advance!

The most important thing to understand is that + is string concatenation while using a comma simple prints multiple arguments. The result might look the same, but the working is different

you could simplify the concatenation a lot by making the spaces part of the string:

"This person’s insurance cost is " + str(new_insurance_cost)

but personally, I would use f-string when available (pyhton3.6 and later), and otherwise use .format():

https://pyformat.info/

4 Likes

that is very useful, thankyou!

I’m new and don’t understand any lessons or anything can someone give me advice to learn easyer other than Codecademy Pro?

I’m new as well! :slight_smile: My advice is: take your time and be patient with the progress. I’m quite slow as well. But I keep going at my own pace… and if I don’t understand something I pose the question here in the community :slight_smile: Good Luck!

1 Like