Suppose you have a variable
my_pi which is set to the
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.
"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) + "."
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().