count()函数与center()函数

count()函数
描述:用于统计字符串李某个字符出现的次数。可选参数为在字符串搜索的开始和结束位置
语法:str.count(sub,start=0,end=len(string))
返回值:子字符串在字符串中出现的次数

str='www.runoob.com'
sub='o'
print('str.count('o'):',str.count('sub'))

count()函数描述:用于统计字符串李某个字符出现的次数。可选参数为在字符串搜索的开始和结束位置语法:str.count(sub,start=0,end=len(string))返回值:子字符串在字符串中出现的次数

str="www.runoob.com"
sub='o'
print ("str.count('o') : ", str.count(sub))

sub='run'
print ("str.count('run', 0, 10) : ", str.count(sub,0,10))

center()函数

描述:center()方法返回一个指定宽度width居中的字符串,fillchar为填充的字符。默认为空格

语法:str.center(width[,fillchar])
返回值:返回一个指定的宽度 width 居中的字符串,如果 width 小于字符串宽度直接返回字符串,否则使用 fillchar 去填充。

>>> str = "[www.runoob.com]"
>>> print ("str.center(40, '?!') : ", str.center(40, '?!'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: The fill character must be exactly one character long
原文地址:https://www.cnblogs.com/bashliuhe/p/13178170.html