python学习笔记之基础操作(二)字符串操作(2)

版权声明:所有资料资源均应用作教育用途,请勿用作商业用途 https://blog.csdn.net/qq_38876114/article/details/82995937
#二:替换操作
#replace():替换指定字符串
test = "123abcefsdfa"
print(test.replace("123","___"))

___abcefsdfa
#swapcase():转换大小写:
test = "asdfgVBNMK"
print(test.swapcase())
ASDFGvbnmk
#upper(),lower()
print(test.upper())
print(test.lower())
ASDFGVBNMK
asdfgvbnmk
#三,分割操作
#split():按照指定字符串分割字符,可以指定分割的数量
test = "123,456,789"
print(test.split(","))
print(test.split(",",1))
['123', '456', '789']
['123', '456,789']
#partition()按照指定字符串分割为两部分,包含分隔符
#rpartition(),右面第一个开始分割
print(test.partition(","))
print(test.rpartition(","))
('123', ',', '456,789')
('123,456', ',', '789')
#四:查找操作
#find():找不到返回-1
test = "12345672"
print(test.find("2"))
print(test.rfind("2"))
print(test.find("a"))
1
7
-1
#index同上,不过返回错误找不到的话
print(test.index("2"))
print(test.rindex("2"))
print(test.index("a"))
1
7



---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-17-af7b2106d969> in <module>()
      2 print(test.index("2"))
      3 print(test.rindex("2"))
----> 4 print(test.index("a"))


ValueError: substring not found

猜你喜欢

转载自blog.csdn.net/qq_38876114/article/details/82995937