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 to share