Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 - Fatal编程技术网

Python 如何右对齐一列数字?

Python 如何右对齐一列数字?,python,python-3.x,Python,Python 3.x,我有以下python代码: #This program converts the speeds 60 KPH #through 130 KPH (in 10 kph increments) #to MPH #Global constants START = 60 END = 131 INCREMENT = 10 CONVERSION_FACTOR = 0.6214 def main(): #Print the table headings print('KPH\t\tMPH'

我有以下python代码:

#This program converts the speeds 60 KPH
#through 130 KPH (in 10 kph increments)
#to MPH

#Global constants
START = 60
END = 131
INCREMENT = 10
CONVERSION_FACTOR = 0.6214

def main():
    #Print the table headings
    print('KPH\t\tMPH')
    print('----------------')

    #Print the speeds
    for kph in range(START, END, INCREMENT):
        mph = kph * CONVERSION_FACTOR
        print(kph, '\t\t', format(mph, '.1f'))

#Call the main function
main()
运行此代码会得到以下结果:

KPH     MPH
----------------
60       37.3
70       43.5
80       49.7
90       55.9
100          62.1
110          68.4
120          74.6
130          80.8

如何右对齐第二列,以便更正确地显示结果?

您也可以使用
printf
样式格式化来指定宽度

>>> print('%10.2f' % 1.23456)
      1.12
在您的示例中,您可以使用:

print('%-10i%.1f' % (kph, mph))
使用

结果(对空格使用
):


谢谢你的例子,但我仍然没有得到正确的对齐,即使是你的最后一个例子。
"{:>10.3}".format(12.34)
______12.3