The following display code is implemented in Ipython3
s='i like Python'
#首字母大写
Capitalize ()
1 in [3]: s='i like Python'23 in [4]: s.capitalize () 4 ' I like Python '
#全部转换大写, convert all lowercase
Upper () lower ()
In [6]: S.upper () out['I like PYTHON'
1 in [7]: S.lower ()2'i like Python'
#大写转换, convert the original uppercase to lowercase, lowercase to uppercase
In [8]: s.swapcase () out['I like PYTHON'
#单词首字母大写
In [9]: S.title () out['I like Python'
#居中
In [All]: S.center (#默认是空格Out) [' i like python'
in [+]: S.center ('~') #制定字符串Out ['~~~i like python~~~~ '
s = ' I like Pyhon '
#删除前后空格 (the default is to delete spaces, you can specify the front and back strings to delete)
Strip
in [+]: S.strip () out['i like pyhon'
#删除后边空格
Rstrip
in [+]: S.rstrip () out[' i like pyhon'
#删除左边空格
Lstrip
in [+]: S.lstrip () out['i like pyhon '
#公共方法
#获取字符串元素长度
Len
in [+]: Len (s) out[19]: 17
#查看元素是否以某字符串开头 (return to True,false),
StartsWith
EndsWith
In []: S.startswith (") out[]: True
In []: S.endswith (") out[]: True
#根据元素找索引号 (Returns-1 if not found)
Find
In [23°c]: S.find ('i') out[23]: 2
Index (Error if not found)
in [+]: S.index ('i') out[26]: 2
#统计 (returns 0 if the element is not counted)
Count
In []: S.count ('i') out[29]: 2
#替换
in [+]: s.replace ('like','love') out[' I love pyhon '
Python-based string manipulation