python问题。
我想在两个管道符号之间打印文本,我希望文本居中。
为:
| Name | class | Number | Grade |
由于
答案 0 :(得分:0)
这个怎么样:
name = 'John'
class_ = 'Class 1'
number = '2'
grade = 'Pass'
str = '| {} | {} | {} | {} |'.format(name, class_, number, grade)
str = str.center(80)
print(str)
要获得终端尺寸,请点击以下链接:
Get current terminal size on Linux, Mac, and Windows
编辑1:
以个别字段为中心:
name = 'John'
class_ = 'Class 1'
number = '2'
grade = 'Pass'
nameWidth = 10
classWidth = 10
numberWidth = 10
gradeWidth = 10
name = name.center(nameWidth)
class_ = class_.center(classWidth)
number = number.center(numberWidth)
grade = grade.center(gradeWidth)
str = '|{}|{}|{}|{}|'.format(name, class_, number, grade)
print(str)
编辑2:
a = ['abcd','bc','cdef','d','e']
n = [1,2,3,4,5]
for i,e in zip(a,n):
i = i.center(5)
e = str(e).center(1) # This line is not needed when width is 1
s = '|{}|{}|'.format(i, e)
print(s)