String methods - str()


#1

What's the difference between putting a str() command and not putting a str() command?
For example, I have these code below:

  1. pi = 3.14
  2. print str(pi)

vs

  1. "pi" = 3.14
  2. print pi

Could someone please explain me the difference between these two since the output is the same? Thanks!


#2

Line numbers are not part of your code.
The second example will not run, you cannot assign to string literals.

To print anything at all, it has to be converted to a string representation. print already does this for you.

str is not a method, it's a class. Calling it creates a string. str() produces an empty string. Giving it an argument will cause it to try to represent the argument as a string, and give you back the result.