python中使用format()方法格式化數(shù)字設(shè)置右對(duì)齊:<(默認(rèn))左對(duì)齊、>右對(duì)齊、^中間對(duì)齊、=(只用于數(shù)字)在小數(shù)點(diǎn)后進(jìn)行補(bǔ)齊
>>>print('{}and{}'.format('hello','world'))#默認(rèn)左對(duì)齊
helloandworld
>>>print('{:10s}and{:>10s}'.format('hello','world'))#取10位左對(duì)齊,取10位右對(duì)齊
helloandworld
>>>print('{:^10s}and{:^10s}'.format('hello','world'))#取10位中間對(duì)齊
helloandworld
>>>print('{}is{:.2f}'.format(1.123,1.123))#取2位小數(shù)
1.123is1.12
>>>print('{0}is{0:>10.2f}'.format(1.123))#取2位小數(shù),右對(duì)齊,取10位
1.123is1.12
以上內(nèi)容為大家介紹了python怎么讓數(shù)字右對(duì)齊?希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。