Why use str(8) vs. "8" to convert numbers to a string?


#1

One of the exercises asks you to use str() to convert a number into a string. Wouldn't " " accomplish the same thing? If not, why wouldn't the " " approach work?


#2

str() is useful when you work with the variables, take a look:

x = 8

str(x)
# => "8"

"x"
# => "x"

#3

We would not write literals since they are not dynamic. The str() method takes variable data and acts upon it. This is the typical data flowing into and out of a program.

number = 42
numstr = str(number)

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.