在python科研程式設計中,字串擴充通常在處理檔名、資料集列表、或列印模型訓練過程資訊時非常實用,
尤其是列印模型訓練過程資訊時,通常需要在每一輪之間列印一行符號表示分隔,此時字串擴充的功能尤其好用,
它能夠省去手動輸入字元的時間,使得輸出字串更加工整。
1. str.center(width[, fillchar])
返回一個原字串居中,並使用空格填充至長度 width 的新字串。預設填充字元為空格。
點選檢視程式碼
a = 'lingdu'
b = a.center(20)
c = a.center(20, '*')
print(b)
print(c)

2. str.ljust(width[, fillchar])
返回一個原字串左對齊,並使用空格填充至指定長度的新字串。如果指定的長度小於原字串的長度則返回原字串。
點選檢視程式碼
a = 'lingdu'
b = a.ljust(20)
c = a.ljust(20, '*')
print(b)
print(c)

3. str.rjust(width[, fillchar])
返回一個原字串右對齊,並使用空格填充至指定長度的新字串。如果指定的長度小於原字串的長度則返回原字串。
點選檢視程式碼
a = 'lingdu'
b = a.rjust(20)
c = a.rjust(20, '*')
print(b)
print(c)

4. str.zfill(width)
返回指定長度的字串,原字串右對齊,前面填充0。
點選檢視程式碼
a = 'lingdu'
b = a.zfill(20)
print(b)
