Python 2: Explicit String Conversions

I am on exercise 13 on “Strings & Console Output” in Python 2.

Code :

#Turn 3.14 into a string on line 3!

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


When I test their example of a string with a number, I wrote it as:

print “I have 2 coconuts!”

They want it written as:
print “I have " +str(2) + " coconuts!”

What’s the difference if they both print the same line out in the console?

if the number comes from user input, a database or file and stored in a variable, your approach does not work.

You will run into this issue more, a concept is explained, doesn’t seem to make much sense or add much value, but you will later see why sometimes what has been taught is needed

also, using + for string concatenation is also slightly outdated. There are much better ways (like .format(), which hopeful will be taught later)

1 Like

I see! That makes sense. Thank you :slight_smile:

1 Like