Read the input values

n = int(input()) message = input()

Split the message into words

words = message.split()

Initialize an empty list to store the lines

lines = [[] for _ in range(n)]

Iterate over the words and add them to the lines alternatively

for i, word in enumerate(words): line_index = i % n lines[line_index].append(word)

Print the lines

for line in lines: print(' '.join(line))

Python: Print Words Alternately on N Lines

原文地址: https://www.cveoy.top/t/topic/pc8B 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录