Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

chmod777

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?

  • one year ago
  • one year ago

  • This Question is Open
  1. zarzelius
    Best Response
    You've already chosen the best response.
    Medals 0

    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

    • one year ago
  2. topnewtech
    Best Response
    You've already chosen the best response.
    Medals 1

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

    • one year ago
  3. chmod777
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  4. topnewtech
    Best Response
    You've already chosen the best response.
    Medals 1

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

    • one year ago
  5. RoamingBlue
    Best Response
    You've already chosen the best response.
    Medals 1

    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

    • one year ago
  6. chmod777
    Best Response
    You've already chosen the best response.
    Medals 0

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

    • one year ago
  7. topnewtech
    Best Response
    You've already chosen the best response.
    Medals 1

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

    • one year ago
  8. RoamingBlue
    Best Response
    You've already chosen the best response.
    Medals 1

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

    • one year ago
  9. chmod777
    Best Response
    You've already chosen the best response.
    Medals 0

    a plus sign

    • one year ago
  10. RoamingBlue
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • one year ago
  11. RoamingBlue
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

    • one year ago
  12. bwCA
    Best Response
    You've already chosen the best response.
    Medals 1

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

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.