Python字符串中心()方法

Python字符串 中心() 方法创建并返回用指定字符填充的新字符串。

null

语法:

绳子。中心(长度[,填充字符])

参数:

  • 长度: 填充字符后字符串的长度。
  • fillchar: (可选)需要填充的字符。如果未提供,则将空格作为默认参数。

返回:

返回一个填充了指定fillchar的字符串,它不会修改原始字符串。

示例1:带有默认fillchar的center()方法

python

# 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: 带有#fillchar的center()方法

python

# 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#####
© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享