Python float to string Conversion Using 10 Different Methods

Hello geeks and welcome in this article, we will cover Python float to string conversion. We will look at different methods through which we can perform this conversion. To do so, we will look at various examples involving different methods. But before moving that ahead, let us try to get a brief overview of the 2 data types.

First, let us look at the float; this kind of data type returns floating-point numbers like- 3.45,8.9, etc. This data type also returns the value for a string of numbers such as “8”, “6”, etc. Now coming to the other data type, which is a string. String data type we can understand it as an array of byte-like Unicode characters. The next section will start looking at different methods to perform the conversion.

Ways to Convert float to string in Python

  1. Basic float to string conversion
  2. Python float to string using list comprehension
  3. Using list comprehension + join() + str()
  4. Converting float to string using join() + map() + str()
  5. Using NumPy
  6. By using the format()
  7. Using String formatting
  8. Python float to string by repr()
  9. Using list() + map()

Let’s see each of them in-depth with the help of examples.

1. Basic float to string conversion

a=10.0
print(type(a))
print(a)
a=str(10.0)
print(type(a))
print(a)

Output:

<class 'float'>
10.0
<class 'str'>
10.0

Above, we have performed a very basic float to string conversion. Here we can notice that 1st we have defined a variable ‘a’. To check its data type, we have used a print statement with a type tag. The output confirms our data type is float. We have used the str tag to change the data type to a string. Once again, we have used the type tag to find the data type. Here the output confirms that we have successfully performed the conversion.

2. list comprehension

This method is preferably used to convert the NumPy float array to an array of string elements. In general, list comprehension can be understood as less code more effective. Let us look at how it is done. Then we will look at the detailed explanation of the process. But at first, let us look at the syntax for it.

["%.2f" % i for i in array_name]
import numpy as ppool
a=ppool.array([1.8,2.6,3.2,4.3,5.1])
print(a)
b=["%.2f" % i for i in a]
print(b)

Output:

[1.8 2.6 3.2 4.3 5.1]
['1.80', '2.60', '3.20', '4.30', '5.10']

Above, we can see that we can perform a successful conversion. Here to do so, we have imported the NumPy library. After which, we have declared an array of float type. Then for the conversion purpose, we have used our syntax. Finally, we have used the print statement, which verifies the conversion.

3. list comprehension + join() + str()

In this particular method, the floating numbers are first converted to a list. After which, they are converted to a string. This method requires a combination of different methods, as mentioned in the heading.

sample = [5.8, 9.6, 10.2, 45.3, 6.0] 
print( str(sample),type(sample)) 
new = " ".join([str(i) for i in sample]) 
print(str(new),type(new))

Output:

[5.8, 9.6, 10.2, 45.3, 6.0] <class 'list'>
5.8 9.6 10.2 45.3 6.0 <class 'str'>

Here we have performed another successful conversion. We can also notice that the conversion follows the list as stated above “Float -> List -> String.” Here the str method is used to convert to the string data type. The join method takes all the methods in iterable and joins them to make a string.

4. join() + map() + str()

To perform this process this operation, the following methods are used. In this process, first, the float is converted to a string. After which, it is converted into a master string.

sample = [5.8, 9.6, 10.2, 45.3, 6.0] 
print( str(sample),type(sample)) 
new = " ".join(str(sample)) 
new2 = " ".join(map(str,sample)) 
print(str(new),type(new))
print(str(new2),type(new2))

Output:

[5.8, 9.6, 10.2, 45.3, 6.0] <class 'list'>
[ 5 . 8 ,   9 . 6 ,   1 0 . 2 ,   4 5 . 3 ,   6 . 0 ] <class 'str'>
5.8 9.6 10.2 45.3 6.0 <class 'str'>

We will have used the same problem as in the list comprehension + str() + join(). We can also see the sequence “Float-> str->Master str.”

5. Using NumPy

import numpy
x = 18.987
print(type(x))
x = numpy.format_float_positional(x)
print(type(x), x)

Output:

<class 'float'>
<class 'str'> 18.987

Here we can see that we have performed the conversion using the NumPy module. To do so, we have used the format_float_positional function. What it does is that converts a floating-point scalar as a decimal string.

6. Using format()

a = 15.66
print(type(a))
a = format(a, 'f')
print(type(a), a)

Output:

<class 'float'>
<class 'str'> 15.660000

Here, we have successfully performed the conversion using the format() method. The format is a string formatting method of Python. This method helps us by linking elements within a string through positional formatting.

7.Using String formatting

We can also perform the conversion using the string formatting method. Python uses a string formatting format similar to the C language, and It is more of an old method.

x = 15.66
x = '%f' % x
print(type(x), x)

Output:

<class 'str'> 15.66000

See again; we have performed a successful conversion. Here we have used the “%f ” format specifier to instruct where to substitute the value of x.

8. Using repr()

x=19.176
print(type(x))
x = repr(x)
print(type(x), x)

Output:

<class 'float'>
<class 'str'> 19.176

Here we have used the repr() method. The general syntax for this method is repr(obj). On completion of the program, this method returns a printable representational string.

9. Using list() + map()

x = [1.1, 2.2, 3.3]
print(type(x))
x = str(list(map(str, x)))
print(type(x),x)

Output:

x = [1.1, 2.2, 3.3]
print(type(x))
x = str(list(map(str, x)))
print(type(x),x)

A few methods involved list and map methods separately, as we have already discussed. This method collectively requires the application of 2.

10.Using f-strings

The basic idea behind this method is to make string interpolation simpler. This is a modern method and easy to use. let us see how it is executed

a=15.67
print(type(a))
b=f"{a:.2f}"
print(type(b))
<class 'float'>
<class 'str'>

Must Read

Conclusion

In this article, we covered Python float to string conversion. We looked at 9 four different methods to use to do so. As per your need and choice, you can use any 9 methods.

I  hope this article was able to clear all doubts. But in case you have any unsolved queries feel free to write them below in the comment section. Done Reading this, why not read about Type error next.

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Some Dude
Some Dude
3 years ago

You forgot the most modern and most convenient way: f-strings
f”{a:.2f}” . Once you are used to calling variables directly in the string, you never want to go back to C-style percentage signs.