Join a List of Integers into a String in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Table of Contents

  1. Join a list of integers into a string in Python
  2. Join a list of integers into a string using a generator expression
  3. Join a list of integers into a string using a list comprehension
  4. Join a list of integers into a string using a for loop

# Join a list of integers into a string in Python

To join a list of integers into a string:

  1. Use the map() function to convert the integers in the list to stings.
  2. Call the str.join() method on a string separator.
  3. Pass the map object to the join() method.
main.py
my_list = [1, 2, 3, 4, 5] my_str = ', '.join(map(str, my_list)) print(my_str) # ๐Ÿ‘‰๏ธ "1, 2, 3, 4, 5"

join list of integers into string

The code for this article is available on GitHub

The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

main.py
my_list = [1, 2, 3, 4, 5] # ๐Ÿ‘‡๏ธ ['1', '2', '3', '4', '5'] print(list(map(str, my_list)))

We simply passed each integer to the str() class to get a map object that only contains strings.

The string the join() method is called on is used as the separator between elements.

main.py
my_list = [1, 2, 3, 4, 5] my_str = '-'.join(map(str, my_list)) print(my_str) # ๐Ÿ‘‰๏ธ "1-2-3-4-5"

If you don't need a separator and just want to join the iterable's elements into a string, call the join() method on an empty string.

main.py
my_list = [1, 2, 3, 4, 5] my_str = ''.join(map(str, my_list)) print(my_str) # ๐Ÿ‘‰๏ธ "12345"

This approach also works if your list contains both strings and integers.

main.py
my_list = [1, 'a', 2, 'b', 3, 'c', 4, 'd', 5] my_str = ', '.join(map(str, my_list)) print(my_str) # ๐Ÿ‘‰๏ธ "1, a, 2, b, 3, c, 4, d, 5"

Alternatively, you can pass a generator expression to the join() method.

# Join a list of integers into a string using a generator expression

This is a three-step process:

  1. Call the join() method on a string separator.
  2. Pass a generator expression to the join() method.
  3. On each iteration, pass the list item to the str() class to convert it to a string.
main.py
my_list = [1, 2, 3, 4, 5] result = ', '.join(str(item) for item in my_list) print(result) # ๐Ÿ‘‰๏ธ "1, 2, 3, 4, 5"

join list of integers into string using generator expression

The code for this article is available on GitHub
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

We used a generator expression to convert each item to a string by passing it to the str() class.

# Join a list of integers into a string using a list comprehension

A list comprehension can also be used in place of a generator expression.

main.py
my_list = [1, 2, 3, 4, 5] result = ', '.join([str(item) for item in my_list]) print(result) # ๐Ÿ‘‰๏ธ "1, 2, 3, 4, 5"

join list of integers into string using list comprehension

The code sample is very similar to the previous one, however, we used a list comprehension.

main.py
my_list = [1, 2, 3, 4, 5] list_of_strings = [str(item) for item in my_list] print(list_of_strings) # ๐Ÿ‘‰๏ธ ['1', '2', '3', '4', '5'] print('-'.join(list_of_strings)) # ๐Ÿ‘‰๏ธ 1-2-3-4-5

On each iteration, we convert the current integer to a string and return the result.

# Join a list of integers into a string using a for loop

A for loop can also be used to convert the list of integers to a list of strings before joining them.

main.py
my_list = [1, 2, 3, 4, 5] list_of_strings = [] for item in my_list: list_of_strings.append(str(item)) my_str = ', '.join(list_of_strings) print(my_str) # ๐Ÿ‘‰๏ธ "1, 2, 3, 4, 5"

join list of integers into string using for loop

The code for this article is available on GitHub

We declared a new variable that stores an empty list and used a for loop to iterate over the original list.

On each iteration, we convert the current item to a string and append the result to the new list.

The last step is to join the list of strings with a separator.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev