字符串魔法(七个基本函数)

1.join()    :用某个符号将字符串中的每个子字符串隔开,如图用空格将weitaoge隔开

test1= "weitao"
test2 =" ".join(test1)
print(test2)
w e i t a o

2.split()    :将字符串分割 :如图中以“a”作为分隔符,1表示以第一个“a”作为分隔符。.rsplit()表示从右开始数

test = "chenachenachen"
v = test.split("a",1)
print(v)
【“chen”,"chenachen"

3.strip()   :去除左右两边的空格或者是/t和/n。.rstrip()只去除右边的。.lstrip()只去除左边的

test = " chen "
v = test.strip()
print(v)

4.find()   :寻找字符串中子字符串的个数。诺一个都没有结果为-1

test = "chenweitao"
v = test.find("e")
print(v)
结果:2

5.upper()   :将字母都改成大写。

6.lower()  :将字母都改成小写。

7.replace()   :用test2去代换test1,其中a:1   b:2   c:3

test1 = "123"
test2 = "abc"
test3 = "123456789"
test4 = test3.replace(test1,test2)
print(test4)

 

原文地址:https://www.cnblogs.com/chenweitao/p/11202291.html