1.把首写字母修改为大写
s = "!hello world@abc"
print(s.title())
# 输出结果 !Hello World@Abc
2.截取字符串,从下标n开始,到m结束包头不包尾,s[n:] 则是从n到结尾
s = "!hello world@abc"
n = 2
m = 6
print(s[n:m])
# 输出结果 ello
3.原样输出字符串
s = "!hello world"
print(s[:])
# 输出结果 !hello world
4.重复输出n次字符串
s = "!hello world@abc"
n = 3
print(s*n)
# 输出结果 !hello world@abc!hello world@abc!hello world@abc
5.倒叙输出字符串
s = "!hello world@abc"
print(s[::-1])
# 输出结果 cba@dlrow olleh!
6.字符串替换
# 把字符串中的rgExp替换成replace,从左边开始替换num次
# 如果num为空则全部替换如写成ss.replace(rgExp,replace)
ss = 'aaBBccDDaaBBccDD'
rgExp = 'BB'
replace = 'EE'
num = 1
print(ss.replace(rgExp,replace,num))
# 输出结果 aaEEccDDaaBBccDD
7.去除字符串头指定字符或字符串
# 参数为空则去除\r, \t, \n, 空格等字符
ss = 'aaBBccDDaaBBccDD'
print(ss.lstrip('a'))
# 输出结果 BBccDDaaBBccDD
8.去除字符串尾指定字符或字符串
# 参数为空则去除\r, \t, \n, 空格等字符
ss = 'aaBBccDDaaBBccDD'
print(ss.rstrip('D'))
# 输出结果 aaBBccDDaaBBcc
9.去除字符串头尾指定字符或者字符串
# 参数为空则去除\r, \t, \n, 空格等字符
ss = 'AaBBccDDaaBBccDA'
print(ss.strip('A'))
# 输出结果 aBBccDDaaBBccD
10.转义特殊字符
# 字符串中有特殊字符 '、"、\n、\t、\r,我需要把这些字符原样输出,这时就可以用\进行转义
ss = 'ab\tcd'
print(ss) # 输出结果 ab cd
ss = 'ab\\tcd'
print(ss) # 转义后输出结果输出结果 ab\tcd
# 把字符串中所有特殊字符都进行转义
ss = 'ab\tcb\nef'
print(ss)
# 输出结果 ab cb
# df
ss = r'ab\tcb\nef'
print(ss)
# 输出结果 ab\tcb\nef
11.格式化输出字符串
# 使用占位符 格式化字符串 %d %s %f
print('num = %d,str = %s,f = %.2f'%(3, 'cpx', 4.5))
# 输出结果 num = 3,str = cpx,f = 4.50
# 使用format格式化字符串
ss = 'num = {},str = {},f = {}'.format(5, 'cpx', 5.3)
print(ss)
# 输出结果 num = 5,str = cpx,f = 5.3
常用函数还有很多,字符串常用函数,文档里面都可以找到