Python | Convert a list of strings to a space delimited string



Examples :

  Input:  [`geeks`,` for`, `geeks`]  Output:  geeks for geeks  Input:  [`Python`,` Language`]  Output:  Python Language 

Approach # 1: Python string translate ()

The string translate () method returns a string that maps each character to the corresponding character in the translation table. A limitation of this method is that it does not work for Python 3 and later.

# Python3 list conversion program
# lines per line separated by space

 

def convert (lst):

  

return str ( lst) .translate ( None , `[],` `)

  
# Code driver

lst = [ `geeks` , ` for` , `geeks` ]

print (convert (lst))

Exit:

 geeks for geeks 

Approach # 2: join ()
The join () method is a string method and returns a string in which the elements of the sequence have been concatenated with the str separator. In this approach, space is the delimiter.

# Python3 list conversion program
# lines into line separated by space

 

def convert (lst):

 

return `` . join (lst)

 
# Driver code

lst = [ ` geeks` , `for` , ` geeks` ]

print (convert (lst))

Exit:

 geeks for geeks