center()方法创建并返回一个用指定字符填充的新字符串。
用法:
string.center(length[, fillchar]) 参数: length:length of the string after padding with the characters. fillchar:(optional) characters which needs to be padded. If it's not provided, space is taken as default argument. 返回: returns a string padded with specified fillchar and it doesn't modify the original string.
代码1
# Python program to illustrate
# string center() in python
string = "geeks for geeks"
new_string = string.center(24)
# here filchar not provided so takes space by default.
print "After padding String is:", new_string
输出:
After padding String is: geeks for geeks
代码2
# Python program to illustrate
# string center() in python
string = "geeks for geeks"
new_string = string.center(24, '*')
# here fillchar is provided
print "After padding String is:", new_string
输出:
After padding String is:****geeks for geeks*****
相关用法
注:本文由纯净天空筛选整理自pawan_asipu大神的英文原创作品 string center() in python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。