Python

Python Float to String

In this guide, we will cover how Python converts a float to a string. We’ll go through different workarounds through which we can complete this task. We’ll discuss various example codes with different approaches. But before moving forward, let us discuss a summary of float and string data types. Float is a data type that returns floating-point values such as 1.4, 7.9, etc.

However, if this data type works with a string of numbers, then its return values like “8”, “6”, etc. On the other hand, the string data type is a collection of byte-like Unicode characters. The next segment will start looking for various approaches to perform this conversion.

Example 1

In this section, you’ll learn the elementary and straightforward methods to perform the conversion.  In the below program code, we first declare a variable. To check its class type, we used the “type” function in the print statement. The type tag states the data or class type of the variable. In our next section, we use the “str” function. For this purpose, we have used the type tag just like we used earlier to check the data or class type of the string. In the end, the print statement prints the resultant output on the console screen.

first_num=11.2
print(type(first_num))
print(first_num)
first_num=str(11.2)
print(type(first_num))
print(first_num)

Here, the result confirms that we’ve successfully converted python float to string.

Example 2

In our next segment, we’ll discuss the list comprehension method to perform the conversion. This workaround is rather used to alter the NumPy float array. In list comprehension, you don’t need to write too many lines of code. Few lines of code make it more efficient. Come, let’s briefly check how it is done.

Above, we perform a successful float to string conversion via a basic method. Here in list conversion, we’ve first imported the NumPy class. After which, we’ve defined an array of float data types. To convert float to string, we have simply used a pre-defined syntax. In the end, the print statement displays the result and successfully confirms the conversion.

import numpy as ppool
f_num=ppool.array([1.8,3.2,2.6,9.3,6.1])
print(f_num)
s_num=["%.2f" % i for i in f_num]
print(s_num)

Here, the result confirms that the list comprehension method successfully converts Python float to string.

Example 3

Other than the basic list comprehension method, here comes the second form of list comprehension approach which works with join() and str() methods. In this illustration, we’ll discuss the list comprehension method with join and str. This workaround is preferably used to first convert floating numbers into a list and then it is converted into a string. This technique needs a mixture of different workarounds.

To convert float to string, we first create a list of floating-point numbers and then use a print statement to print that list. In this statement, we’ve defined two tags i.e., str and type. The str function is utilized to alter the float number into string data type and the “type” tag states the class type. In our next step, we call the join() function which takes all the approaches in iterable and then combines them to make a string. Finally, the print statement prints the result in string data type.

l_1 = [4.8, 9.6, 7.2, 95.3, 1.0]
print( str(l_1),type(l_1))
new_str = " ".join([str(x) for x in l_1])
print(str(new_str),type(new_str))

The result confirms that list comprehension with join() and str() methods successfully converts Python float to string.

Example 4

Other than the aforementioned methods, here comes another interesting method of ‘f-string’ to convert floats into strings. The basic impression behind this technique is to make string utterances easier. This is another simple and easy-to-use method. Come, let’s check how to implement this method.

In our first illustration, we’ll first define a floating-point variable and then print that variable with its type. After this, we define another variable with the f-string function and then use a print statement to print the number with its type.

num1 =14.65
print(type(num1))
num2 =f"{num1:.2f}"
print(type(num2))

Again, run the f-string code and check the output on the screen.

Conclusion

Here, we discussed several ways to convert a float to a Python string. We discussed basic, list comprehension(), list comprehension with join() & str(), and f-strings methods to successfully perform our conversion.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content