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)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
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.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
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;
@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.
I mean that the print don't support string and integer object
you can test this
print "x" 2
>>> total = 15
>>> print "TOTAL", total
TOTAL 15 ### the comma, int syntax adds a space between the values
>>> total = 15
>>> print "TOTAL"+str(total) ### straight string concatenation does not
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: