Monday, November 18, 2024
Google search engine
HomeLanguagesPython | List of float to string conversion

Python | List of float to string conversion

Sometimes, while working with Python lists, we can have a problem in which we have to convert float to string and if we have a list of float data then list float elements into strings, that’s called float-to-string conversion in Python.

Input: f = [22.23, 45.6, 9.35, 23.45]
Output: '22.23' '45.6' '9.35' 23.45'
Explanation: Initially we have the float data-type value which is converted into string with the str() method

Conversion of Float to String

Below are the lists of methods that we will cover in this article:

Float to string conversion using str() method

Python has the str() method inbuilt which we can use to convert the float data type into a string and for a list of data we can first iterate through the data and change the data type through iteration into float and then print the result

Python3




lst = [22.23, 45.6, 9.35, 23.45]
ans = []
for i in lst:
   
  ans.append(str(i))
print('Every element in ans is of string data-type :')
print(*ans)


Output:

Every element in ans is of string data-type :
22.23 45.6 9.35 23.45

Time Complexity: O(n)
Auxiliary Space: O(n)

Convert Float to String using list comprehension and join()

This task can be performed using a combination of the above functions. In this, we first convert each element of the list into a string through iteration using list comprehension and then print the result i.e. float point number to a string, and then join the resultant strings using the join()

Python3




# initialize list
test_list = [5.8, 9.6, 10.2, 45.3, 6.0]
 
# printing original list
print("The original list is : " + str(test_list))
 
# List of float to string conversion
res = " ".join([str(i) for i in test_list])
 
# printing result
print("Conversion of float list to string : " + str(res))


Output

The original list is : [5.8, 9.6, 10.2, 45.3, 6.0]
Conversion of float list to string : 5.8 9.6 10.2 45.3 6.0

Time Complexity: O(n) where n is the number of elements in the list “test_list”. list comprehension + join() + str() performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list

Convert Float to String using format() in for loop

This method uses a for loop to iterate through the list and the format() function to convert each element of the list to a string and then print the result.

Python3




# initialize list
test_list = [5.8, 9.6, 10.2, 45.3, 6.0]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Convert float list to string
res = ""
for i in test_list:
    res=res+("{:}".format(i))+" "
 
# printing result
print("Conversion of float list to string : " + str(res))


Output

The original list is : [5.8, 9.6, 10.2, 45.3, 6.0]
Conversion of float list to string : 5.8 9.6 10.2 45.3 6.0 

This method is similar to the previous method, but it uses a for loop to iterate through the list. It allows more control over the iteration process.

Time Complexity: O(n)
Auxiliary Space: O(n)

Python Float to String Without Decimal

Here we can use the int() method to remove the decimal after the float value and then we will use the str() method to convert every value of the list into a string inside the for loop and print the result with values in string data type.

Python3




lst = [22.23, 45.6, 9.35, 23.45]
ans = []
for i in lst:
  #frist converting to int data-type then into string
  ans.append(str(int(i)))
print('Every element of the list is of string data-type:')
print(*ans)


Output:

Every element of the list is of string data-type:
22 45 9 23

Time Complexity: O(n)
Auxiliary Space: O(n)

Convert floating point number to a certain Precision

This line of code uses list comprehension to create a new list f_rounded. It iterates over each element number in the original list f.

The f-string formatting expression {number:.{precision}f} is used to format each element number. Here, number is the value from the original list, and precision is the number of decimal places we want to display and after it we print the result

Python3




# Original list
f = [22.23, 45.6, 9.35, 23.45]
 
# Round each element of the list to a certain precision (e.g., 1 decimal places) using f-string
precision = 1
f_rounded = [f"{number:.{precision}f}" for number in f]
print("The output have only one digit after the decimal point :")
print(f_rounded)


Output:

The output have only one digit after the decimal point :
['22.2', '45.6', '9.3', '23.4']

Time Complexity: O(n)
Auxiliary Space: O(n)

Rounding floats value to a string with f-string

In this, we take the round to nearest value in Python into a string by using the round method which is inbuilt in Python to convert the float value to its round to nearest and then convert it into the string using f-string and then print the result

Python3




# Original list
f = [22.23, 45.6, 9.35, 23.45]
 
# Round each element of the list to a certain precision (e.g., 2 decimal places) using f-string
f_rounded_strings = [f"{round(number)}" for number in f]
 
print("Here the output have the round string value of every float digit :")
print(f_rounded_strings)
print(*f_rounded_strings)


Output:

Here the output have the round string value of every float digit :
['22', '46', '9', '23']
22 46 9 23

Time Complexity: O(n)
Auxiliary Space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments