Does str( ) change the value stored in the variable?


#1

Using conversion functions like str() and float() does not change the value stored in a variable unless you assign the variable the value of itself converted, which is totally possible! Take a look at the code below for an example of this idea:

my_var = 10.0  
print “This is my_var’s value: “ + str(my_var)
my_var = int(my_var)
print “Now my_var is an int! Here it is: “ + str(my_var)
my_var = str(my_var) 
print “Now I can print it as normal because it’s a string: “ + my_var

Our variable starts off as a float, then we print it using str() to temporarily convert it so it can be concatenated with a string. Then it is actually assigned to the int version of itself. Finally, it becomes a string and we don’t need to convert it because you can concatenate strings together with no issue.


FAQ: Learn Python - Python Syntax - ValueError
#2

No it does not. All it does is changes the numeric value of a variable to a string.