Given a list of floats:
my_list = [0.3, 0.11, -0.9]
I want a string literal object:
my_string = "0.3, 0.11, -0.9"
Attempt:
print(', '.join(inputs))
> TypeError: sequence item 0: expected str instance, float found
Given a list of floats:
my_list = [0.3, 0.11, -0.9]
I want a string literal object:
my_string = "0.3, 0.11, -0.9"
Attempt:
print(', '.join(inputs))
> TypeError: sequence item 0: expected str instance, float found
The compact way:
l = [0.3, 0.11, -0.9]
print (str(l)[1:-1])
Output:
0.3, 0.11, -0.9
Still, to be used with caution :). The "regular" way would be:
print (', '.join([str(x) for x in l]))
Arguably, the easiest way would be to do:
my_string = str(my_list)[1:-1]
This takes my_list, converts it into a string, and takes the characters from 1)leaving the first character at 0) to -1 (the end character is not taken)
A more pythonic way is to use map to first convert all the float values to str, and then use the function join to concatenate into a string.
print(', '.join(map(str, my_list)))
This prints 0.3, 0.11, -0.9 when my_list = [0.3, 0.11, -0.9]
you can use join function of string like this:
first you have to convert float to str
my_string = ', '.join([str(i) for i in my_list])