Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python program to Concatenate all Elements of a List into a String

  • Last Updated : 11 Dec, 2020

Given a list, the task is to write a Python program to concatenate all elements in a list into a string.

Examples:

Input: ['hello', 'geek', 'have', 'a', 'geeky', 'day']
Output: hello geek have a geeky day

Input: ['did', 'u', 'like', 'my', 'article', '?']
Output: did u like my article ?

Method 1: Using join() method

Python3




# code
l = ['hello', 'geek', 'have',
   'a', '1', 'day']
  
# this will join all the 
# elements of the list with ' '
l = ' '.join(l) 
print(l)

Output:

hello geek have a 1 day

Method 2: Naive approach

Python3




l = [ 'hello', 'geek', 'have'
     'a', 'geeky', 'day']
  
ans = ' '
for i in l: 
    
  # concatenating the strings
  # using + operator
  ans = ans+ ' '+i
    
print(ans)

Output:

hello geek have a geeky day

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!