Python 字符串处理常用方法

常用Python内部字符串处理函数

与其他语言一致,python对于字符处理也存在较多的内部处理函数,今天列举出常用的字符串处理方法:

  1. 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
  1. 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
  1. 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
  1. 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
  1. 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
  1. 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
  1. 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
  1. 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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值