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


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

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


No because it just representation of value as you see in integer type you get only 10 the value of .0 is skip but in string just show the original value but its size does not change


I was tinkering with this for a while, I used to code it like this :

float_1 = 0.25
float_2 = 40.0
product = int(float(float_1) * float(float_2))
big_string = "The product was " + str(product)

By Your advice my code would look like this :

float_1 = 0.25
float_2 = 40.0
product = float(float_1) * float(float_2)
product = int(product)
big_string = "The product was " + str(product)

I think it is much better to do it like yours, so when tracking back we could see every sequences in the code. But it all depends on the person working on it. Thanks