Python – Convert List of floats into List of strings

Python – Convert List of floats into List of strings

To convert a given list of floats into a list of strings in Python, you can use List comprehension or For loop to iterate over the elements in original list, and then use str() built-in function to convert each float element into a string element.

Now, we shall go through some examples of converting a given list of floating point values into list of strings using list comprehension, or a For loop.

1. Converting list of floats into list of strings using List comprehension and str()

Consider that we are given a list of float elements, and we have to convert them into a list of strings using List comprehension and str() builtin function.

Steps

  1. Given a list of float values in my_list.
  2. Use list comprehension, and for each element in the original list, apply str() function to the element.
[str(x) for x in my_list]
  1. The above expression returns a list of strings created from the original list. Assign it to a variable, say list_str.
  2. You may print the resulting list of strings to the standard output using print() statement.

Program

The complete program to convert the given list of float values into a list of strings using List comprehension and str() built-in function.

Python Program

# Given a list of floats
my_list = [1.23, 2.05, 3.14, 4.7]

# Convert list of floats into list of strings
list_str = [str(x) for x in my_list]

print(list_str)
Run Code Copy

Output

['1.23', '2.05', '3.14', '4.7']

2. Converting list of floats into list of strings using For loop, list append() and str()

Consider that we are given a list of float elements, and we have to convert them into a list of strings using For loop, list append() method, and str() built-in function.

Steps

  1. Given a list of floats in my_list.
  2. Take an empty list to store the resulting list of strings, say list_str.
  3. Use a For loop to iterate over the given list, and for each element in the given list, convert the float element into a string element, and append the string element to list_str using list append() method.
for x in my_list:
  list_str.append(str(x))
  1. After the For loop is done, list_str contains the respective string values created from the original list.

Program

The complete program to convert the given list of floating point values into a list of strings using For loop, list append() function, and str() built-in function.

Python Program

# Given a list of floats
my_list = [1.23, 2.05, 3.14, 4.7]

# Take an empty list to store the strings
list_str = []

# Convert list of floats into list of strings
for x in my_list:
    list_str.append(str(x))

print(list_str)
Run Code Copy

Output

['1.23', '2.05', '3.14', '4.7']

Summary

In this tutorial, we have seen how to convert a given list of floats into a list of strings, in two approaches, with examples. The first one was using List comprehension and str() built-in function. The second approach was using For loop, list append() method, and str() built-in function.

Related Tutorials

Code copied to clipboard successfully 👍