center() 方法返回在長度寬度居中的字串。填充是使用指定 fillchar 完成。默認 filler 是一個空格。
語法
str.center(width[, fillchar])
參數
-
width -- 這是字串的總寬度
-
fillchar -- 這是填充符
返回值
此方法返回一個字串,至少是 width 字元寬度, 通過用填充字元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