Python center()方法

Python 字串 Python 字串


描述

Python center() 返回一個原字串居中,並使用空格填充至長度 width 的新字串。默認填充字元為空格。

語法

center()方法語法:

str.center(width[, fillchar])

參數

  • width -- 字串的總寬度。
  • fillchar -- 填充字元。

返回值

該方法返回一個原字串居中,並使用空格填充至長度 width 的新字串。

實例

以下實例展示了center()方法的實例:

>>>str = 'zaixian' >>> str.center(20, '*') '*******zaixian*******' >>> str.center(20) ' zaixian ' >>>

Python 字串 Python 字串