Using quotation marks instead of the str() method?


If I want to concatenate and print the string "The value of pi is" and the integer "3", instead of writing

print "The value of pi is" + str(3.14)

can I write

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


Do the quotation marks turn integers into strings? If so, why would I ever have to use the str() method??



The str() method is used to convert numbers into strings
You are right, your two lines of code produce the same output.
How will you print a value of a variable?

pi = 3.14
print "The value of pi is " + str(pi)
print "The value of pi is " + "pi"

Which line will print the desirable outcome?
That is why you will ever have to user the str() method


Oh, that makes a lot of sense. Thank you for your explanation!


For this problem, can't we just type
print "The value of pi is"+pi


It is very python to type it in the interpreter and see what happens :wink: