Explicit String Conversion


#1

Having some trouble on this one
Im putting print "I have " + 3.14 + " coconuts!" yet it say its wrong
What do I do :confused:


#2

What are the exact instructions? :smile:


#3

Sometimes you need to combine a string with something that isn't a string. In order to do that, you have to convert the non-string into a string.
print "I have " + str(2) + " coconuts!"

This will print I have 2 coconuts!.

The str() method converts non-strings into strings. In the above example, you convert the number 2 into a string and then you concatenate the strings together just like in the previous exercise.

Now try it yourself!

Instructions

  1. Run the code as-is. You get an error!
  2. Use str() to turn 3.14 into a string. Then run the code again.

?

Hint

The code to turn the number 2 into a string is str(2).

Can you convert the number 3.14 into a string?


#4

Use the str() method, like they did for "2".

str(3.14)

do you see why?


#5

omg it worked, thank u so much :smiley: !


#6

thx amanuel2 its works now


#7

Paste your code. Plus please delete that post, one of the words may be taken offensively. Thankyou


#8

Oops, try again. Gadzooks! A string and a float cannot be concatenated. Here's what Python says is the error: cannot concatenate 'str' and 'float' objects


#9

can some body plz hlp me http://discuss.codecademy.com/t/explicit-string-conversion/14309

1

Explicit String Conversion

1

Explicit String Conversion


#11

I am getting "TypeError: 'str' object is not callable" in the exercise described above. As in,
print "The value of pi is around " + str(3.14)


#12

follow the script of my friends ^,^
print " The value of pi is around" + str ( 3.14)


#14