Python字串center()方法

Python字串center()方法以字串寬度(width)為中心。使用指定的填充字元(fillchar)填充完成。默認填充字元(fillchar)是一個空格。

語法

以下是center()方法的語法 -

str.center(width[, fillchar])

參數

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

返回值

  • 此方法返回一個至少寬度寬的字串,它是通過用字符fillchar填充字串(默認為空格)創建。

示例

以下示例顯示了center()方法的用法。

#!/usr/bin/python3

str = "this is string example....wow!!!"

print ("str.center(40, 'a') : ", str.center(40, 'a'))

當運行上面的程式,它產生以下結果 -

str.center(40, 'a') :  aaaathis is string example....wow!!!aaaa

上一篇: Python字串 下一篇: Python列表