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
>>>
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.