center()方法返回集中在長度寬度的字符串。填充是通過使用specifiedfillchar。默認填充字符是一個空格。
語法
以下是center()方法的語法:
1
|
str .center(width[, fillchar]) |
參數(shù)
- width -- 這是字符串的總寬度。
- fillchar -- 這是填充符。
返回值
此方法返回集中在長度寬度的字符串。
例子
下面的示例演示center()方法的使用。
1
2
3
4
5
|
#!/usr/bin/python str = "this is string example....wow!!!" ; print "str.center(40, 'a') : " , str .center( 40 , 'a' ) |
當(dāng)我們運行上面的程序,它會產(chǎn)生以下結(jié)果:
1
|
str .center( 40 , 'a' ) : aaaathis is string example....wow!!!aaaa |