Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

chmod777

  • 2 years ago

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?

  • This Question is Open
  1. zarzelius
    • 2 years ago
    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

  2. topnewtech
    • 2 years ago
    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

  3. chmod777
    • 2 years ago
    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.

  4. topnewtech
    • 2 years ago
    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

  5. RoamingBlue
    • 2 years ago
    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

  6. chmod777
    • 2 years ago
    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

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

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

  8. RoamingBlue
    • 2 years ago
    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?

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

    a plus sign

  10. RoamingBlue
    • 2 years ago
    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.

  11. RoamingBlue
    • 2 years ago
    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.

  12. bwCA
    • 2 years ago
    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

  13. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

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.