第一期 Python 程式設計入門共學營作業任務五


今天要跟大家介紹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)

程式設計實作題作業網址

程式設計實作題

#第一期Python程式設計入門共學營






你可能感興趣的文章

[ 筆記 ] 網路基礎 - HTTP, Request, Response

[ 筆記 ] 網路基礎 - HTTP, Request, Response

D35_W4 HW1 + HW2

D35_W4 HW1 + HW2

DE2_115(DAY2)用niosii和switch還有NiosII console去控制板子上的led

DE2_115(DAY2)用niosii和switch還有NiosII console去控制板子上的led






留言討論