How to print a space between each letter of a word in a list on a new line?

Viewed 1599

What I want

sentence = ["This","is","a","short","sentence"]

# Desired Output

T h i s
i s
a
s h o r t
s e n t e n c e
>>>

What I tried

sentence = [row.replace(""," ") for row in sentence]

for item in sentence:
    print(item)

The problem with this is that it prints a space at the beginning and end of every line but I only want a space between each letter

2 Answers

You could use str.join()

sentence = ["This","is","a","short","sentence"]

for w in sentence:
    print(' '.join(w))

You could use the facts that a string is a sequence, a sequence can be split into its items with the splat * operator, and and the print function prints items by default separated by spaces. Those three facts can be combined into one short line, print(*word), if word is a string. So you can use

sentence = ["This","is","a","short","sentence"]

for word in sentence:
    print(*word)

This gives the printout

T h i s
i s
a
s h o r t
s e n t e n c e
Related