The simplest and most efficient way to convert a float (double) value to a string in Python is to use the built-in str() function or f-string (Python 3.6+).
Method 1: Using str()
It converts an input to its string representation using a default precision (approximately up to 15 decimal places, trimming trailing zeros).
float_value = 3.14159 print(float_values) # Output: 3.14159 print(type(float_value)) # Output: <class 'float'> string_value = str(float_value) print(string_value) # Output: "3.14159" print(type(string_value)) # Output: <class 'str'> # Negative number negative_float = -21.19 print(negative_float) # Output: -21.19 print(type(negative_float)) # Output: <class 'float'> string_float = str(negative_float) print(string_float) # Output: "-21.19" print(type(string_float)) # Output: <class 'str'>
In this code, we passed a positive float value and a negative float value, and in both cases, it returns the respective string.
We can verify the data type using the type() method. Before the conversion, the type of a variable was <class ‘float’>, and after the conversion, the type is <class ‘str’>.
Extra large/small numbers
If the input is a very large or small floating value, it returns the shortest string (16 digits) that, when converted back to a float, yields the exact binary representation.
big_float_value = 3.1415926535897932384626433832795 print(type(big_float_value)) # Output: <class 'float'> max_string = str(big_float_value) print(max_string) # Output: '3.141592653589793' print(type(max_string)) # Output: <class 'str'> small_float_value = 1.23e-10 print(type(small_float_value)) # Output: <class 'float'> min_string = str(small_float_value) print(min_string) # Output: '1.23e-10' print(type(min_string)) # Output: <class 'str'>
Here, you can see that when we pass a value with scientific notation, such as “e”, it returns the output in that exact format.
Special values (NaN, Infinity)
What if the input values are NaN or infinity? In this case, it returns the string representation of nan and inf values, which are case sensitive.
import math
nan_val = math.nan
print(type(nan_val))
# Output: <class 'float'>
inf_val = float('inf')
print(type(inf_val))
# Output: <class 'float'>
print(str(nan_val))
# Output: 'nan'
print(type(str(nan_val)))
# Output: <class 'str'>
print(str(inf_val))
# Output: 'inf'
print(type(str(inf_val)))
# Output: <class 'str'>
neg_inf = float('-inf')
print(type(neg_inf))
# Output: <class 'float'>
print(str(neg_inf))
# Output: '-inf'
print(type(str(neg_inf)))
# Output: <class 'str'>
Method 2: Using f-string
The f-string was introduced in Python 3.6 as a straightforward way to format a string. With its help, we can easily control the formatting, such as specifying the number of decimal places, width, or notation, when converting a double to a string.
f_value = 3.1415926535
print(f_value)
# Output: 3.1415926535
print(type(f_value))
# Output: <class 'float'>
string_frmt = "{:.2f}".format(f_value)
print(string_frmt)
# Output: '3.14'
print(type(string_frmt))
# Output: <class 'str'>
Padding
We can add spaces to the input value while converting to a string and format it using an f-string.
num = 42.38
print(num)
# Output: 42.38
print(type(num))
# Output: <class 'float'>
padded_string = "{:10.2f}".format(num)
# Right-aligned, width 10
print(padded_string)
# Output: ' 42.38' (added spaces)
print(type(padded_string))
# Output: <class 'str'>
That’s all!



