How to Convert Float to String in Python?

In Python, different data types are used to represent, organize and modify the data. The decimal number values in Python are represented by “float”. Similarly, the “string” is represented in Python by the sequence of characters, numbers, etc., enclosed in single or double quotation marks.

To convert float to string, the following methods are used in Python. These methods will be discussed in detail using appropriate examples:

Method 1: Using str() Function

In the example code given below, the “str()” function is used to convert the float into a string:

Code:

value = str(45.56)
print(value)
print(type(value))

In the above code, the “str()” function accepts the float value as an argument and returns the string. The “type()” function is used to check the type of the converted string by accepting the “value” variable as an argument.

Output:

The “45.26” float value has been converted into string using the “str()” function.

Method 2: Using the f-string Method

The “f-string” method is concise and efficient for formatting strings in Python. The f-string method is prefixed with the letter “f” and adds the expressions inside string literals using {}. The expressions inside the curly braces will be replaced with the values of the variable. For further understanding, let’s have a look at the below code:

Code:

value = 45.5676578
output = f'{value:.3f}'
print(output)
print(type(output))

In the above code, the “f-string” method is used with the decimal specifier “.3f” which will convert the given float value into a string with specified decimal points.

Output:

The float value has been converted into the string based on the specified decimal specifier.

Method 3: Using repr() Function

To represent the object representation in string format, the “repr()” function is used in Python. In the example code given below, the “repr()” function is used to convert the float to string:

Code:

value = 63.2323
output = repr(value)
print(output)
print(type(output))

In the above code, the “repr()” function accepts the float as an argument and returns the string object.

Output:

The above output shows the string representation of the input float value, i.e., “63.2323”.

Method 4: Using format() Function

The “format()” function is commonly used for formatting strings in Python. In the example below, the “format()” function accepts a format specifier as an argument and converts the float to a string accordingly.

Code:

value = 19.6752
output = format(value, 'f')
print(output)
print(type(output))

In the above code, the “format()” function uses the character value “f” to convert the input float value to a string.

Output:

This output shows that the given float value was successfully converted to a string.

Method 5: Using format_float_positional() Function

The Python “Numpy‘ module provides the “numpy.format_float_positional()” function to convert the given float to a string. Let’s understand it via the following code:

Code:

import numpy
value = 32.876
output = numpy.format_float_positional(value)
print(output)
print(type(output))

In the above code, the “numpy.format_float_positional()” function of the “Numpy” module is used to convert the float to a string in Python.

Output:

The above output verifies that the float value “32.876” is converted into a string value.

Method 6: Using List Comprehension

The “List Comprehension” method can also be used along with the “for” loop to convert the list containing floats to a string in Python.

Code:

value = [22.3,44.5,66.7,32.5]
str_value = [str(item) for item in value]
output = " ".join(str_value)
print(output)
print(type(output))

In the above code, the “str()” function converts each item/value of a given list into strings by iterating over each value of the list. The “join()” function accepts the string as an argument and joins the value to an empty string.

Output:

The given list containing multiple float values has been converted into a string using the list comprehension method.

Conclusion

To convert float to string, the “str()” function, “f-string” method, “repr()”, and “format()” functions, Numpy, and List Comprehension, are used in Python. The “str()” function takes the float as a parameter value and converts it into a string. The formatted string methods such as “f-string” and “format()” are also used to represent the given float to string in Python. This Python tutorial presented a detailed guide on converting float values to a string via suitable examples.