Concatenate to a string in a for loop in Python

avatar

Borislav Hadzhiev

Last updated: Aug 29, 2022

banner

Photo from Unsplash

Concatenate to a string in a for loop in Python #

To concatenate to a string in a for loop:

  1. Declare a variable and initialize it to an empty string.
  2. Use a for loop to iterate over the sequence.
  3. Reassign the variable to its current value plus the current item.
main.py
my_list = ['a', 'b', 'c', 'd'] my_str = '' # ✅ Concatenate to a string using for loop for item in my_list: my_str += item print(my_str) # 👉️ 'abcd' # -------------------------------------------------- # ✅ Join the strings in a list using str.join() | <- better result = ''.join(my_list) print(result) # 👉️ 'abcd'

The first step is to declare a new variable and initialize it to an empty string.

On each iteration in the for loop, we reassign the variable to its current value plus the value of the current list item.

main.py
my_list = ['a', 'b', 'c', 'd'] my_str = '' # ✅ Concatenate to a string using for loop for item in my_list: my_str += item print(my_str) # 👉️ 'abcd'
The += operator is a shorthand for my_str = my_str + item.

The following code snippet achieves the same result.

main.py
my_list = ['a', 'b', 'c', 'd'] my_str = '' for item in my_list: my_str = my_str + item print(my_str) # 👉️ 'abcd'

Alternatively, you can use the str.join() method.

main.py
my_list = ['a', 'b', 'c', 'd'] result = ''.join(my_list) print(result) # 👉️ 'abcd'

Using the str.join() method is a bit more efficient in this scenario.

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.

If your list contains numbers or other types, convert all of the values to string before calling join().

main.py
my_list = ['a', 'b', 1, 2] all_strings = list(map(str, my_list)) print(all_strings) # 👉️ ['a', 'b', '1', '2'] result = ''.join(all_strings) print(result) # 👉️ "ab12"

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

main.py
my_list = ['a', 'b', 'c'] my_str = '-'.join(my_list) print(my_str) # 👉️ "a-b-c"
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 = ['a', 'b', 'c'] my_str = ''.join(my_list) print(my_str) # 👉️ "abc"

You can use a generator expression if you need to concatenate only certain items of the list.

main.py
my_list = ['?', 'a', '-', 'b', 'c', 'd', '!'] result = ''.join(item for item in my_list if item.isalpha()) print(result) # 👉️ 'abcd'

The example uses the str.isalpha() method to only join the alphabetic characters in the list.

The str.isalpha() method returns True if all characters in the string are alphabetic and there is at least one character, otherwise False is returned.

We used a generator expression to iterate over the list.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current list item contains only letters and return the result.

The join() method joins the matching elements into a string.

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.