How to Convert a Float List to a String List in Python

The most Pythonic way to convert a list of floats fs to a list of strings is to use the one-liner fs = [str(x) for x in fs]. It iterates over all elements in the list fs using list comprehension and converts each list element x to a string value using the str(x) constructor.

This article shows you the simplest ways to convert a one-dimensional list consisting only of floats to a list of strings.

Problem: Given a list of floats [1.0, 2.0, 3.0]. How to convert it to a list of strings ["1.0", "2.0", "3.0"]?

Method 1: List Comprehension

Suppose we have a list:

a = [1.1, 1.2, 1.8, 0.5, 5.9, -2.3]

Now, check the type of the list:

print(type(a[0]))
# <class 'float'>

Let’s apply the built-in function str(), and get a list of strings using list comprehension:

print([str(a) for a in a])
# ['1.1', '1.2', '1.8', '0.5', '5.9', '-2.3']

πŸ’‘Β List comprehension is a compact way of creating lists. The simple formula is [expression + context]. Expression: What to do with each list element? Context: What elements to select? The context consists of an arbitrary number of for and if statements.

You can watch me explain list comprehensions in this video:

Check the type of numbers in the new list:

A = [str(a) for a in a]
print(type(A[0]))
# <class 'str'>

The built-in function str converts a float to a string representation of that float. Thus, it helps us create a new list of strings from the list of floats in a single line of code.

🌍 Recommended Tutorial: How to Convert a Float to a String in Python?

Method 2: Map Function

The built-in function map is well optimized and efficient; when it is called, the elements of the list are retrieved upon access. Therefore, one element is stored and processed in memory at a time, so the program doesn’t need to store the entire list of elements in the memory.

Apply to the same list a the following code:

a = [1.1, 1.2, 1.8, 0.5, 5.9, -2.3]
print(list(map(str, a)))
# ['1.1', '1.2', '1.8', '0.5', '5.9', '-2.3']

The map() function returns an iterable map object that we need to convert to a list using the built-in list() constructor.

Method 3: For Loop

Of course, you can also convert a list of floats to a list of strings using a simple for loop. This is what most people coming from a programming language such as Java and C++ would do as they don’t know the most Pythonic way of using list comprehension, yet (see Method 1).

a = [1.1, 1.2, 1.8, 0.5, 5.9, -2.3]
strings = []

for element in a:
    strings.append(str(element))

print(strings)
# ['1.1', '1.2', '1.8', '0.5', '5.9', '-2.3']

This basic method to convert a list of floats to a list of strings uses three steps:

  • Create an empty list with strings = [].
  • Iterate over each float element using a for loop such as for element in list.
  • Convert the float to a string using str(element) and append it to the new string list using the list.append() method.

Method 4: String Formatting for Custom String Conversions

If this is not enough for you, for instance, you need a specific format of the converted strings such as only two digits after the decimal point, you should have a look at Python’s powerful string formatting capabilities.

For example, to convert a list of floats with many digits to a list of strings with only two digits, use the string.format() method:

a = [1.11111, 1.200023402, 1.844333, -0.5000001]
strings = ['{:.2f}'.format(x) for x in a]
print(strings)
# ['1.11', '1.20', '1.84', '-0.50']

πŸ’‘Β Python’s built-in format(value, spec) function transforms the input of one format into the output of another format as defined by you. Specifically, it applies the format specifier spec to the argument value and returns a formatted representation of value. For example, format(42, 'f') returns the string representation '42.000000'.

You can watch me introduce the formatting capabilities in this short guide:

Bonus Method 5: f-String

A more concise way to use Python’s formatting functionality can be accessed via f-strings in Python 3 and beyond. Convert a float list to a string list by using list comprehension and the float-to-string conversion expression f'{x:.2f}' on each list element x.

Here’s an example:

a = [1.11111, 1.200023402, 1.844333, -0.5000001]
strings = [f'{x:.2f}' for x in a]
print(strings)
# ['1.11', '1.20', '1.84', '-0.50']

Note you can increase or decrease the number of digits by setting the format specifier accordingly:

  • f'{x:.2f}' yields two digits such as '3.12'
  • f'{x:.3f}' yields two digits such as '3.123'
  • f'{x:.4f}' yields two digits such as '3.1234'

To boost your Python skills the easy way, feel free to join my free email academy with lots of free content and cheat sheets—if you haven’t already! πŸ™‚

Programmer Humor

It’s hard to train deep learning algorithms when most of the positive feedback they get is sarcastic. — from xkcd

If you want to go all-in and learn Python while getting paid in the process, check out my Python freelancer course—the number one freelance developer education in the world!