码迷,mamicode.com
首页 > 编程语言 > 详细

Python String Methods

时间:2017-11-08 10:34:07      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:字符串   空格   方法   size   字符串搜索   blog   style   长度   har   

str.capitalize()  # 将字符串的第一个字母变成大写,其他字母变小写。对于 8 位字节编码需要根据本地环境
>>> ‘hello‘.capitalize()
‘Hello‘
>>> ‘hEllo‘.capitalize()
‘Hello‘
string.center(width[, fillchar]) # 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串。默认填充字符为空格
>>> ‘hello‘.center(10,‘0‘)
‘00hello000‘
>>> ‘hello‘.center(10)
‘  hello   ‘
string.count(sub, start= 0,end=len(string)) #方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置
>>> ‘hello world‘.count(‘o‘)
2
>>> ‘hello world‘.count(‘o‘,4,8)
2
>>> ‘hello world‘.count(‘o‘,4,7)
1

 

Python String Methods

标签:字符串   空格   方法   size   字符串搜索   blog   style   长度   har   

原文地址:http://www.cnblogs.com/njuptlwh/p/7802621.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!