Python | List of float to string conversion

Sometimes, while working with Python list, we can have a problem in which we have to transform the list elements to string. This is easier in case of integral list, as they can be joined easily with join(), and their contents can be displayed. But the case with floats is different, there are additional undesired spaces among it’s values that can cause it’s unsuccess. Let’s discuss a ways in which this error situation can be handled.

Method #1 : Using list comprehension + join() + str()
This task can be performed using combination of above functions. In this, we firstly convert each element of list i.e float point number to string and then join the resultant strings using the join().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# List of float to string conversion
# using list comprehension + str() + join()
  
# 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
# using list comprehension + str() + join()
res = " ".join([str(i) for i in test_list])
  
# printing result
print("Conversion of float list to string : " + str(res))

chevron_right


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

 

Method #2 : Using join() + map() + str()
The root method is similar by using the combination of join() + str(), but important function which helps to perform this task is map(). This first converts each element to string and then constructs the master string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# List of float to string conversion
# using join() + map() + str()
  
# 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
# using join() + map() + str()
res1 = " ".join(str(test_list))
  
# List of float to string conversion
# using join() + map() + str()
res2 = " ".join(map(str, test_list))
  
# printing result
print("Conversion using join + str : " + str(res1))
print("Conversion using join + str + map : " + str(res2))

chevron_right


Output :

The original list is : [5.8, 9.6, 10.2, 45.3, 6.0]
Conversion using join + str : [ 5 . 8,   9 . 6,   1 0 . 2,   4 5 . 3,   6 . 0 ]
Conversion using join + str + map : 5.8 9.6 10.2 45.3 6.0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.