Why use str() instead of quote mark


#1



https://www.codecademy.com/courses/python-beginner-sRXwR/3/2?curriculum_id=4f89dab3d788890003000096


Its more about a question on why I cant use quotes marks to convert a number into string, rather than use the str() operator. I tried using it and the interpreter gave the same result.
instead of using print "The value of pi is around " + str(3.14)
if I use: print "The value of pi is around "+"3.14"
I get the same results; so whats the difference here? why should I go with str() operator?


print "The value of pi is around "+"3.14"


#2

this exercise is about teaching a concept, which is how to cast to a string. Lets say for whatever reason i need a random integer:

from random import randint
x=randint(0,9)

now our random integer is stored in a variable named x. How would you get from integer to string? using str()

so yea, if possible you can just put quotation marks around the number, but this is not always possible


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.