Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 固定字段宽度的打印_Python_Python 3.x_Format - Fatal编程技术网

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}'