Python program to concatenate a list of characters to a string

How to concatenate a list of characters to a string:

In this post, we will learn how to concatenate a list of characters to a string in python. We can either iterate through the items one by one and concatenate them to a string. Also, we have other ways that can be used to do this.

Method 1: By iterating throught the characters in the list:

This is the basic approach. We can use one for loop, iterate through the characters in the list and append them to a string. Once the loop will end, the string will hold the final result.

given_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
final_str = ''

for ch in given_list:
    final_str += ch

print('Final string : {}'.format(final_str))

Here,

  • given_list is the given list of characters.
  • final_str is the final string that will hold the concatenate value of all characters.
  • Using a for in loop, we are iterating through the list of characters and adding each value to final_str.
  • The last line is printing the value of final_str

If you run this program, it will look as like below:

python concatenate char list to string

Method 2: Using str.join():

join method, defined in python string takes one iterable as its argument and joins all items in that iterable to a string. If we pass a list of characters, it joins all characters in the list to a string.

If we write the above program in str.join, it will look as like below:

given_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
final_str = ''.join(given_list)

print('Final string : {}'.format(final_str))

It will print the same output.

Method 3: Using reduce():

reduce is another method that can be used to concatenate one list of characters to a string. We can use add that is defined in operator.

from operator import add 
from functools import reduce

given_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
final_str = reduce(add, given_list)

print('Final string : {}'.format(final_str))

It will give the same output.

You might also like: