Hi, I'm doing 6.00sc the 2011 version. I noticed in some of the code from the lecture and in hand outs that when ever they are printing a variable which is an int or float embedded in the middle of a string, they first convert it into a string using the funtion str(x): eg. >>> print "The total amount is ", str(total), "gallons."; I'm wondering why they do this, when it would be easier to just use the variable unconverted since it would print anyway. Or is there some rule I'm unaware of?

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions.

A community for students.

Hi, I'm doing 6.00sc the 2011 version. I noticed in some of the code from the lecture and in hand outs that when ever they are printing a variable which is an int or float embedded in the middle of a string, they first convert it into a string using the funtion str(x): eg. >>> print "The total amount is ", str(total), "gallons."; I'm wondering why they do this, when it would be easier to just use the variable unconverted since it would print anyway. Or is there some rule I'm unaware of?

MIT 6.00 Intro Computer Science (OCW)
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this and thousands of other questions

I was thinking the same.Either way is going to print the variable as it is, or at least that is how c++ works so im confused about this as well.I just started and im in lecture 3.Ill try to work on the problem set starting tomorrow, altho ive taking a look at it and they look a bit tough indeed XD
I think that in python, you can not concat a string with an integer until you are making them both string; Good Luck
@topnewtech but you could type this and it would still work: >>> print "The total amount is ", total, "gallons."; I do not think it's concatenating, but rather printing multiple obejcts without going to a new line. I'm a beginner myself so it wou;d be great if someone experienced could weigh in.

Not the answer you are looking for?

Search for more explanations.

Ask your own question

Other answers:

I mean that the print don't support string and integer object you can test this print "x" 2 Good luck
Note that >>> total = 15 >>> print "TOTAL", total will print TOTAL 15 ### the comma, int syntax adds a space between the values whereas >>> total = 15 >>> print "TOTAL"+str(total) ### straight string concatenation does not will print TOTAL15 It can be done either way, they just behave a bit differently
but why waste time using the str() function when you can use the variable as-is
thank you @RoamingBlue I made a mistake, @chmod777 my reponse it's false
are they using a comma to separate when they're using the str() function, or a plus sign?
a plus sign
The answer is: It's a matter of preference: you can use commas to concatenate an integer into the string or plus signs and the str() function or you can use explicit formatting (which I haven't seen them introduce yet) The downside to commas is they introduce spaces in print's output, which you may not want. If you use plus signs and only put spaces in the output where YOU want them, you have to use the str() function on ints.
Though it would be an interesting side experiment to set a timer and add 1000 numbers into a string one one and do it the other way, and see if one or the other consistently performed better.
or even use the string format() method: http://dpaste.com/791741/ http://docs.python.org/library/string.html#format-string-syntax

Not the answer you are looking for?

Search for more explanations.

Ask your own question