v32.docx - Converting Numbers to Strings Sometimes you need to convert a number to a string You might do this for example if you need to build a string

v32.docx - Converting Numbers to Strings Sometimes you need...

This preview shows page 1 out of 1 page.

Converting Numbers to Strings Sometimes you need to convert a number to a string. You might do this, for example, if you need to build a string from some pre-existing variables that are assigned to numeric values. 92 4.6. Working With Strings and Numbers As you’ve already seen, the following produces a TypeError: >>> num_pancakes = 10 >>> "I am going to eat " + num_pancakes + " pancakes." Traceback (most recent call last): File "", line 1, in TypeError: can only concatenate str (not "int") to str Since num_pancakes is a number, Python can’t concatenate it with the string "I'm going to eat". To build the string, you need to convert num_pancakes to a string using str(): >>> num_pancakes = 10 >>> "I am going to eat " + str(num_pancakes) + " pancakes." 'I am going to eat 10 pancakes.' You can also call str()
Image of page 1

You've reached the end of your free preview.

Want to read the whole page?

  • Spring '15

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes