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


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?


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

x = 8

# => "8"

# => "x"


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)


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