Change language

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 

Python | Convert a list of strings to a space delimited string __del__: Questions

Python | Convert a list of strings to a space delimited string Python functions: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method