markdown使用語法
阿新 • • 發佈:2021-02-07
技術標籤:python
格式轉換
方法:1.format
2.%
3,f
a=[1,2]
print(f'{a[1]}')
在格式化字元前面加上f就可以直接引用同名變數
格式化的應用
根據輸入字元的寬度列印價格列表
from wcwidth import wcswidth
item=input('請輸入商品名稱:')
price=float(input('請輸入商品價格:'))
price_width=wcswidth(str(price))+6
item_width=wcswidth(item)+6
width=price_width+item_width
header='{{:^{}}}{{:^{}}}' .format(item_width,price_width)
fmt='{{:^{}}}{{:^{}.2f}}'.format(item_width,price_width)
print('='*width)
print(header.format('Item','Price'))
print('-'*width)
print(fmt.format(item,price))
print('='*width)