What does Str() do?


#1

https://www.codecademy.com/en/courses/python-beginner-sRXwR/1/4?curriculum_id=4f89dab3d788890003000096#

The code behaves correctly, I just have a question on why it works/what it does

The lesson says that str() "turns non-strings into strings!" but what does that mean and what does it do for me?

pi = 3.14
print str(pi)

And it prints 3.14, but the same outcome would happen if I used the code

pi = 3.14
print pi

without str().

So the lesson really only taught me how to use str() and not what it really does, or how to use it in a proper situation...


#2

It may look like it has the same output but it actually returns a string.

If you were to do something like this:

pi = 3.14
print str(pi) + 4
# You would get 3.144 because it is simply concatenating the 4 to the end of the string (you may or may not get an error trying to add a number to a string)

whereas if we do something like this:

pi = 3.14
print pi + 4
# You would get 7.14 because it is adding the float values together mathematically

#3

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