對齊方式的取值:
-
:左對齊<
-
:右對齊>
-
:居中^
-
:在正負号(如果有的話)和數字之間填充,該對齊選項僅對數字類型有效。它可以輸出類似=
這樣的字元串。+0000120
>>> print("|",format("RUNOOB","*>30"),"|") #左對齊
| ************************RUNOOB |
>>> print("|",format("RUNOOB","*^30"),"|") #居中對齊
| ************RUNOOB************ |
>>> print("|",format("RUNOOB","*<30"),"|") #右對齊
| RUNOOB************************ |
>>>
print("執行開始".center(scale//2,"-")) # .center() 控制輸出的樣式,寬度為 25//2,即 22,漢字居中,兩側填充 -
>>> print("執行開始".center(50//2,"-"))
-----------執行開始----------
>>> '{1:3d}'.format(1, 1)
' 1'
>>>
>>> '{1:3d}'.format(1, 12)
' 12'
>>>
>>> '{1:3d}'.format(1, 123)
'123'
>>> '{1:3d}'.format(1, 12345)
'12345'
>>>
>>> '{1:4d}'.format(12345) #index從0開始
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>>
>>> '{:^3.0f}'.format(3.1415) #占3位,0個小數點
' 3 '
>>> '{:^3.0f}'.format(24.1415)
'24 '
>>> '{:3.0f}'.format(24.1415)
' 24'
>>> '{0:3.0f}'.format(24.1415)
' 24'
>>> '{1:3.0f}'.format(24.1415) #index從0開始
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>>
>>> '{:^20.1f}'.format(3.8,)
' 3.8 '
>>>