首页 > 解决方案 > 如何在Python中连续打印多行文本(一个相邻)

问题描述

所以,如果我得到例如:

print("some text\n
    some more text\n
    yet some more text")

和:

print("another some text\n
    another some more text\n
    yet another some more text")

如何在不这样做的情况下将它们彼此相邻打印:

print("some text         another some text\n
    some more text       another some more text\n
    yet some more text   yet another some more text")

我不想做我刚刚向您展示的内容,因为我将生成更多具有不同文本和值的列,但我希望它们彼此相邻并带有\n中断。

我怎样才能在 Python 中做到这一点?

标签: pythontext

解决方案


一个简单的方法是循环遍历字符串列表,如下所示:

column_1 = ["some text", "some more text", "yet some more text"]
column_2 = ["another some text", "another some more text", "yet another some more text"]

for i in range(0, len(column_1 )):
    print("{}\t{}".format(column_1[i], column_2[i]))

当然,如果两列的长度相同。如果你想更好地对齐它们,你可以更巧妙地使用标签。


推荐阅读