第一期 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程式設計入門共學營






你可能感興趣的文章

ESlint 錯誤訊息紀錄

ESlint 錯誤訊息紀錄

Linux Curl Command 指令與基本操作入門教學

Linux Curl Command 指令與基本操作入門教學

Integration Test on DB-Related Code with Docker Compose

Integration Test on DB-Related Code with Docker Compose






留言討論