今天要跟大家介紹Python字串物件的一個方法str.center(width[, fillchar])
以下是官方文件的介紹:
Return centered in a string of length width. Padding is done using the specified fillchar (default is an ASCII space). The original string is returned if width is less than or equal to len(s).
描述
回傳一個原字串居中,並使用fillchar的字元填充至長度為width的新字串。(fillchar預設為空白)
語法
str.center(width[, fillchar])
參數
- width--字串的總寬度
- fillchar--填充的字元
回傳值
回傳一個原字串居中,並使用fillchar的字元填充至長度為width的新字串
實例
text = 'Python'
center_text = text.center(10, '*')
#以Python為中心,兩邊填充*字元至width總長度等於10個字元
# **Python**
print(center_text)