Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 13 Online
OpenStudy (anonymous):

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?

OpenStudy (anonymous):

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

OpenStudy (anonymous):

I think that in python, you can not concat a string with an integer until you are making them both string; Good Luck

OpenStudy (anonymous):

@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.

OpenStudy (anonymous):

I mean that the print don't support string and integer object you can test this print "x" 2 Good luck

OpenStudy (anonymous):

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

OpenStudy (anonymous):

but why waste time using the str() function when you can use the variable as-is

OpenStudy (anonymous):

thank you @RoamingBlue I made a mistake, @chmod777 my reponse it's false

OpenStudy (anonymous):

are they using a comma to separate when they're using the str() function, or a plus sign?

OpenStudy (anonymous):

a plus sign

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

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.

OpenStudy (anonymous):

or even use the string format() method: http://dpaste.com/791741/ http://docs.python.org/library/string.html#format-string-syntax

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!