How can I format this list to one line?

I have a list that contains a word. Each letter is separated by a space (as shown below).

word = ["h", " ", "e", " ", "l", " ", "l", " ", "o", " "]

      

I am trying to print it in the format:

h e l l o

      

I tried using the print operator (among other things), but it just came out:

["h", " ", "e", " ", "l", " ", "l", " ", "o", " "]

      

How to fix it?

+3


source to share


2 answers


You can str.join(iterable)

combine them into one line:

"".join(word)

      

This will concatenate all the elements of the array with empty strings, essentially concatenating the strings together into one. Then you can print it:



print("".join(word))

      

This will create

h e l l o

      

+2


source


Just use the join function to convert the List to string:

print ("".join(my_word))

      



"" before .join means white space will be added between characters. If you want, you can put whatever you want, even spaces or numbers or strings.

+1


source







All Articles