1.str使用:
全部大写:str.upper()
全部小写:str.lower()
大小写互换:str.swapcase()
首字母大写:str.capitalize()
首字母大写:str.title()
![](https://images2015.cnblogs.com/blog/1112031/201702/1112031-20170224171344913-1329295867.png)
2.关于format:
获取固定长度,右对齐,左边不够用空格补齐:str.rjust(width)
获取固定长度,左对齐,右边不够用空格补齐:str.ljust(width)
获取固定长度,中间对齐,两边不够用空格补齐:str.center(width)
获取固定长度,右对齐,左边不足用0补齐str.zfill(width)
![](https://images2015.cnblogs.com/blog/1112031/201702/1112031-20170224180823898-538079557.png)
3.字符串搜索:
搜索指定字符串,没有返回-1:str.find('t')
指定起始位置搜索:str.find('t',start)
指定起始及结束位置搜索:str.find('t',start,end)
从右边开始查找:str.rfind('t')
搜索到多少个指定字符串:str.count('t')
上面所有方法都可用index代替,不同的是使用index查找不到会抛异常,而find返回-1
a = 'TesT'
![](https://images2015.cnblogs.com/blog/1112031/201702/1112031-20170224183023023-344070371.png)
![](https://images2015.cnblogs.com/blog/1112031/201702/1112031-20170224183033320-1067529314.png)
4.字符串替换相关
替换old为new:str.replace('old','new')
替换指定次数的old为new:str.replace('old','new',maxReplaceTimes)
![](https://images2015.cnblogs.com/blog/1112031/201702/1112031-20170224184624882-558248862.png)
5.字符串去空格及去指定字符
去两边空格:str.strip()
去左空格:str.lstrip()
去右空格:str.rstrip()
去两边字符串:str.strip('d'),相应的也有lstrip,rstrip
![](https://images2015.cnblogs.com/blog/1112031/201702/1112031-20170224185206523-1713709514.png)
![](https://images2015.cnblogs.com/blog/1112031/201702/1112031-20170224185220179-186106061.png)
6.字符串判断相关
是否以start开头:str.startswith('start')
是否以end结尾:str.endswith('end')
是否全为字母或数字:str.isalnum()
是否全字母:str.isalpha()
是否全数字:str.isdigit()
是否全小写:str.islower()
是否全大写:str.isupper()
![](https://images2015.cnblogs.com/blog/1112031/201702/1112031-20170224190018913-43288728.png)