Latest web development tutorials

Python center()方法

Python center()方法

Python 字符串 Python字符串


描述

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

語法

center()方法語法:

str.center(width[, fillchar])

參數

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

返回值

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

實例

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

#!/usr/bin/python

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字符串