The most Pythonic way to convert a float to a string is to pass the float into the built-in str()
function. For example, str(1.23)
converts the float 1.23
to the string '1.23'
.
Here’s a minimal example:
my_float = 1.23 my_string = str(my_float) print(my_string) # '1.23'
Python Float to String Precision (Decimals)
To set the decimal precision after the comma, you can use the f-string formatting functionality in Python 3. For example, the expression f'{x:.3f}'
converts the float variable x
to a float with precision 3.
Here’s an example:
my_float = 1.23456789 my_string = f'{my_float:.3f}' print(my_string) # '1.235'
Note that the float value 1.23456789 is rounded to 1.235 with three digits after the decimal place.
Python Float to String Format
To set the precision after the comma when converting a float to a string in Python, you can also use the string format()
method. For example, the expression '{:.2f}'.format(x)
converts the float variable x
to a float with precision 2 such as 3.13
or 1.23
.
Here’s an example:
my_float = 1.23456789 my_string = '{:.3f}'.format(my_float) print(my_string) # '1.235'
Python Float to String Two (2) Decimal Places
To set the precision after the comma to two decimal places when converting a float to a string, you can use Python’s f-string formatting functionality. For example, the expression f'{x:.2f}'
converts the float variable x
to a float with precision two (“2”) such as 1.23
or 3.14
.
Here’s an example:
my_float = 1.23456789 my_string = f'{my_float:.2f}' print(my_string) # '1.23'
Note that in this case, the remaining digits are rounded down to the next full integer digit. However, in another case, this exact approach would round up:
my_float = 1.239 my_string = f'{my_float:.2f}' print(my_string) # '1.24'
Python Float to String Round
- If you use the
str()
function on a given float, Python does not round, e.g.,str(1.24999)
yields'1.24999'
. - If you use the
f'{my_float:.2f}'
expression, Python converts the float to a string representation of the rounded float withn
digits.
The following example shows both cases:
my_float = 1.24999999 # 1: NO ROUNDING str(my_float) '1.24999999' # 2: ROUNDING f'{my_float:.2f}' '1.25'
Python Float to String Without .0
If you want to convert a float such as 1.0
, 2.0
, or 3.0
to a string without the trailing decimal .0
, first pass the float to the int()
function and pass the resulting integer in the str()
function. For example, the expression str(int(3.0))
converts the float to the string '3'
.
Here’s a minimal example:
my_float = 3.0 my_string = str(int(my_float)) # '3'
Python Float to String Without Decimal
If you want to convert a float to a string without the trailing decimal, pass it to the int()
function and pass the resulting integer in the str()
function. For example, the expression str(int(3.599))
converts the float to the string '3'
.
Here’s a minimal example:
my_float = 3.599 my_string = str(int(my_float)) # '3'
Python Float to String with Comma
To convert a float to a string with comma instead of decimal point, i.e., '3,599'
instead of '3.599'
apply two steps:
- Convert the float to a string with a decimal point by using the
str(my_float)
function. - Convert the decimal point string to a comma representation by using the
string.replace('.', ',')
function.
Here’s an example:
my_float = 3.599 my_string = str(my_float) # '3.599' new_string = my_string.replace('.', ',') # '3,599'
Python Float to String Scientific Notation
To convert a float to a Python string with scientific notation, use the f-string expression f'{my_float:.2E}'
for a float variable my_float
.
Here’s an example:
my_float = 999999999999999999.999999999999 my_string = f'{my_float:.2E}' print(my_string) # 1.00E+18
Thanks for your attention! π If you want to keep learning, feel free to join our email academy (we have cheat sheets!):
A similar problem is converting a float list to a string list in Python. If this is what you want to do, feel free to check out our tutorial:
π Recommended Learning: Convert Float List to String List in Python
And here’s a bit of more lightweight content—some programming humor:
Programming Humor
π‘ Programming is 10% science, 20% ingenuity, and 70% getting the ingenuity to work with the science.
~~~
- Question: Why do Java programmers wear glasses?
- Answer: Because they cannot C# …!
Feel free to check out our blog article with more coding jokes. π