Python字符串的各种方法以及实例

s.capitalize()

make the first character have upper case and the rest lower case.

s.center(width, fillchar=None)

Padding is done using the specified fill character (default is a space).

s.count(sub, start=None, end=None)

从一个范围内统计某str出现的次数


s.encode(encoding='UTF-8', errors='strict') 

encode() 方法以指定的编码格式编码字符串,默认编码为 'utf-8'。将字符串由string类型变成bytes类型


s.endswith(suffix, start=None, end=None)

Return True is s ends with the specified suffix, False otherwise.


s.expandtabs(tabsize=8)

将字符串中包含的\t 转换成tabsize个空格


s.find(sub, start=None, end=None)


s.index(sub, start=None, end=None)

Like s.find() but raise ValueError when the substring is not found


s.isalnum()

至少一个字符,且都是字母或数字才返回True

s.isalpha()

至少一个字符,且都是字母才返回True

s.isdecimal()

Return True if there are only decimal characters is S

s.isdigit()


s.isidentifier()

字符串为关键字返回true

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容