How to Convert Float to String in Python
To convert a float to a string in Python, you can use the str() function or the format() function.
The following examples show how to convert a float to a string in Python using these two methods.
Using str() Function
We can use the str() function to convert a float to a string.
Suppose we have the following float value:
# Declare float data
data = 78.6
# Convert float to string
data = str(data)
# Show result
print(data)
print(type(data))
Output: 👇️
78.6
<class 'str'>
In this example, we use the str() function to convert the float data to a string. The output shows the string value and its type.
Using format() Function
We can use the format() function to convert a float to a string.
Suppose we have the following float value:
# Declare float data
data = 78.6
# Convert float to string
str_value = "{:.1f}".format(data)
# Show result
print(str_value)
Output: 👇️
78.6
In this example, we use the format() function to convert the float data to a string str_value with one decimal place. The output shows the string value.
Conclusion
We can use the str() function or the format() function to convert a float to a string in Python. These methods provide a convenient way to handle numeric conversions from floats to strings.