Using quotation marks instead of the str() method?


#1

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??


#2

Hi,

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


#3

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


#4

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


#5

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