到目前为止,我已经知道了这一点,但我在让输出正确对齐时遇到了问题
我尝试使用.rjust
使我的输出显示如下:
abc
de
fghijklmn
但它的表现是这样的:
abc
de
fghijklmn
以下是我到目前为止所做的尝试:
strings = ['abc', 'de', 'fghijklmn']
col_umn = ("\n".join(strings))
rcol_umn = (col_umn.rjust(max(len(ele) for ele in strings)))
width = max(len(ele) for ele in strings)
fillchar = " "
print(col_umn.rjust(5, fillchar))
length = max(strings)
res = max(len(ele) for ele in strings)
print(" ")
print(str(res))
您可以将字符串格式化为在f字符串中填充和对齐。在本例中,我使用
>
来表示右对齐,并使用longest
的值来告诉它要填充/对齐多少输出
相关问题 更多 >
编程相关推荐