What is the purpose of str() if I can can receive the same results without it?


#1

Hey all!

So… currently learning about Strings/Console Output.

I’ve gotten to the len() + str() + etc section.

So - I understand len() and upper.() and all of that.

But str() is really what is getting me.

What is the purpose?

If I can write…

print "The value of pi is around " + “3.14”

why would I write

print "The value of pi is around " + str(3.14)

They both output the same results.


#2

str is the string type, like with other types you can use it to create an instance of that type, such as when doing conversions

If you already have a string or you’re using it in some way where it would get converted to string anyway, then there’s no benefit in using it


#3

Ahhh alrighty - so I guess that’s more the underlying point here.

When I move forward, I’ll learn of instances where I won’t necessarily always have a definitive string.

So you could just use str() to bypass some of these rules, more or less?


#4

Instance is really just an English word, not something of special significance in python. A string is an instance of the str type, similar to how 5 is an instance of int. A particular lake is an instance of the concept of lake

Not sure what you mean by bypass, it does what it does and is what it is, and if that’s useful to you, then you can leverage it. If you for example have a number and you want to concatenate it with a string, that isn’t something that makes sense at all, but you can get a text representation of that number and then it makes sense to concatenate it with other text.


#5

Suppose you have a variable my_pi which is set to the float 3.14.

my_pi = 3.14

When you need to join the value that is in my_pi with the text string “The value of pi is around”, the pi needs to be a string, too. You cannot join a string with a float because they are of different types.

Try "The value of pi is around " + 3.14 in your Python Shell. You will get a TypeError: must be str, not float.

I will give you a couple examples:

name = 'Bob'
age = 20

I would not set age to the string “20”.

Now you may want to print a message like Bob is 20. Before printing it, the integer value in age needs to be converted to a string:

print name + " is " str(age) + "."

Another example:

x = 10
y = 5
diff = x - y

print "Subtracting " + str(x) + " from " + str(y) + " gives you " + str(diff) + "."

As you go on learning, you will see why and when you need such type conversion.

If you want to learn more about the built-in function, you could go to str().


#6

As exemplified in @gvm’s post, we commonly need to include the value of a variable or expression in an output string. In such a situation, the value of the variable often needs to be converted to a string. Perhaps we have a program that estimates the value of π, and we output the result in a sentence. We cannot just do this …

print("The value of pi is around " + “3.14”)

… because we have the result in a variable. However, we can do something like this …

print("The value of pi is around " + str(pi_estimate))

There are other means of performing the conversion. Here’s another technique that is covered in later Codecademy Python exercises …

print("The value of pi is around %0.8f" % (pi_estimate))

That will give us eight digits after the decimal point, regardless of whether the value we have is accurate to that precision.

There are additional, more recent, mechanisms for formatting output in Python that Codecademy does not cover in its courses.


#7

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