Python 固定字段宽度的打印
这是到目前为止我的代码。它符合我的要求,只是在打印字符串时没有固定的宽度Python 固定字段宽度的打印,python,python-3.x,format,Python,Python 3.x,Format,这是到目前为止我的代码。它符合我的要求,只是在打印字符串时没有固定的宽度 def PrintTuple(tuple) : formatted = '{}, {:<15} {:<5} {:<5} {:<5} £{:<5}'.format(tuple[4], '', tuple[3], tuple[0], tuple[2], tuple[1]) print(formatted) 我希望它能够打印,以便它们具有固定的宽度: 15, 15, 5, 15,
def PrintTuple(tuple) :
formatted = '{}, {:<15} {:<5} {:<5} {:<5} £{:<5}'.format(tuple[4], '', tuple[3], tuple[0], tuple[2], tuple[1])
print(formatted)
我希望它能够打印,以便它们具有固定的宽度:
15, 15, 5, 15, 7
您的格式说明符中有一个杂散的
{}
,并且您的字段宽度与您所说的不同。尝试:
def PrintTuple(tup) :
formatted = u'{:<15} {:<15} {:<5} {:<15} £{:<7}'.format(tup[4], tup[3], tup[0], tup[2], tup[1])
print(formatted)
使用此字符串:
'{}, {:<15} {:<5} {:<5} {:<5} £{:<5}'
{},{:对不起,我不太明白你的意思。你能给我解释一下吗?
PrintTuple((12349, 30000, 'Wizard', 'Harry Potter', 'Potter'))
PrintTuple((13128, 50, 'Timelord', 'Peter Capaldi', 'Capaldi'))
Potter Harry Potter 12349 Wizard £30000
Capaldi Peter Capaldi 13128 Timelord £50
'{}, {:<15} {:<5} {:<5} {:<5} £{:<5}'
'{:<15}, {:<15} {:<5} {:<5} {:<5} £{:<5}'