
Convert a float to a string in Python
You can convert a float to a string using the str()
in Python.
f1 = 1.23
f2 = .5
s1 = str(f1)
s2 = str(f2)
print(s1) # 1.23
print(s2) # 0.5
Both s1
and s2
are Python strings. A float value starting with dot has 0 integer part so .5
means 0.5
.
f1 = 1 / 2
f2 = 1 / 7
s1 = str(f1)
s2 = str(f2)
print(s1) # 0.5
print(s2) # 0.14285714285714285
Comments
Powered by Markdown