常用Python内部字符串处理函数
与其他语言一致,python对于字符处理也存在较多的内部处理函数,今天列举出常用的字符串处理方法:
count()
列举字符串中所有需要查找的字符数量:
a = "abcdefgabcdefg"
print(a.count("a"))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
2
Process finished with exit code 0
find()
从左开始查找字符串内第一个指定字符,并返回索引位置:
a = "abcdefgabcdefg"
print(a.find("a"))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
0
Process finished with exit code 0
3.rfind
从右向左查找字符串内第一个指定字符,并返回索引位置:
a = "abcdefgabcdefg"
print(a.rfind("a"))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
7
Process finished with exit code 0
4.center
按所需填充内容,将字符串居中填充,此函数包含两个入参:
参数1:需要填充的字符数量;
参数2:需要填充的字符
a = "欢迎来到召唤师峡谷"
print(a.center(40,"#"))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
###############欢迎来到召唤师峡谷################
Process finished with exit code 0
lower()
将字符串全部转换成小写:
a = "abcDEfgabCdeFg"
print(a.lower())
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
abcdefgabcdefg
Process finished with exit code 0
upper()
将字符串全部转换成大写:
a = "abcDEfgabCdeFg"
print(a.upper())
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
ABCDEFGABCDEFG
Process finished with exit code 0
startswith()
判断字符串是否已指定内容开始(注意:函数返回值为bool量):
a = "abcDEfgabCdeFg"
print(a.startswith("a"))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
True
Process finished with exit code 0
endswith()
判断字符串是否已指定内容结束(注意:函数返回值为bool量):
a = "abcDEfgabCdeFg"
print(a.endswith("a"))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
False
Process finished with exit code 0
split()
将字符串按指定的字符切割成列表,此函数可以具有两个入参:
参数1: 指定切割的字符内容
参数2: 指定切割的次数(在不设置此参数的 情况下,默认将所有需要替换的内容全部替换为指定内容)
不设置参数2:
a = "abcDEfgabCdeFg"
print(a.split("e"))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
['abcD', 'fgabCd', 'Fg']
Process finished with exit code 0
设置参数2:
a = "abcDEfgabCdeFg"
print(a.split("e",1))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
['abcD', 'fgabCdeFg']
Process finished with exit code 0
10.rsplit()
由右向左将字符串按指定的字符切割成列表,此函数可以具有两个入参:
参数1: 指定切割的字符内容
参数2: 指定切割的次数(在不设置此参数的 情况下,默认将所有需要替换的内容全部替换为指定内容)
不设置参数2:
a = "abcDEfgabCdeFg"
print(a.rsplit("e"))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
['abcD', 'fgabCd', 'Fg']
Process finished with exit code 0
设置参数2:
a = "abcDEfgabCdeFg"
print(a.rsplit("e",1))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
['abcDefgabCd', 'Fg']
Process finished with exit code 0
11.strip()
删除字符串前后的空格或换行:
a = " \nabcDefgabCdeFg\n"
print(a.strip())
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
abcDefgabCdeFg
Process finished with exit code 0
replace()
按要求替换字符串指定内容,此函数可具有三个参数:
参数1:需要替换的内容
参数2:替换成何内容
参数3:替换次数(在不设置此参数的 情况下,默认将所有需要替换的内容全部替换为指定内容)
不设置参数3:
a = "abcDefgabCdeFg"
print(a.replace("e","1"))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
abcD1fgabCd1Fg
Process finished with exit code 0
设置参数3:
a = "abcDefgabCdeFg"
print(a.rsplit("e","1",1))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
abcD1fgabCdeFg
Process finished with exit code 0
以上为所有常用的字符串处理方式。
补充一个,字符串与字符串拼接:
join
方式拼接字符串优于使用+
拼接字符串:
x = "haha"
y = "enen"
print("".join([x,y]))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
hahaenen
Process finished with exit code 0
注意:此方式不仅使用与直接拼接,可以按照需求已怎样的方式连接.
如:按-
连接字符串:
x = "haha"
y = "enen"
print("-".join([x,y]))
结果:
C:\Users\gaoqifa\AppData\Local\Programs\Python\Python36\python.exe C:/Users/Desktop/study/test.py
haha-enen
Process finished with exit code 0