字符串基础
>>>'hello,%s!' % ('mo')
'hello,mo!'
>>>'{foo}{bar}'.format(bar=4,foo=3)
'3 4'
>>>'{2}{1}{3}{0}'.format(a,b,c,d)
'c b d a'
>>>'{}{}{}{}'.format(1,2,4,3)
'1 2 4 3'
>>>'{:.2f}'.format(pi)
'3.14'
>>>a='{{:{}}}{{:>{}.2f}}'.format(25,10)
>>>print(a.format('apple',1.3333))
'apple 1.33'
>>>width=int(input('Enter the width: '))
...price_wid=10
...item_wid=width-price_wid
...header ='{{:{}}}{{:>{}}}'.format(item_wid,price_wid)
...content='{{:{}}}{{:>{}.2f}}'.format(item_wid,price_wid)
...print('='*width)
...print(header.format('item','price'))
...print('-'*width)
...print(content.format('apple',1.3333))
...print(content.format('orange',2.45678))
...print('-'*width)
Enter the width: 30
==============================
item price
------------------------------
apple 1.33
orange 2.46
------------------------------