购买主题
沙发
本节讲解了字符串的“查找”和“替换”两种类别下的方法。其中translate()方法略微有点复杂,其参数是用于指定一个转换规则的表格,需要用字符串的静态方法str.maketrans()来生成。
有趣的是,不仅使用空格的代码比使用Tab的代码更多,而且习惯使用空格的程序员的平均薪资居然也比习惯使用Tab的程序员要高出不少!
{:10_266:}{:10_266:}{:10_266:}{:10_266:}
复习:
Python 3.9.1 (tags/v3.9.1:1e5d33e, Dec7 2020, 17:08:21) on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> x = "上海自来水来自海上"
>>> x.count("海")
2
>>> x.count("海",,)
SyntaxError: invalid syntax
>>> x.count("海",0,5)
1
>>> x.find("海")
1
>>> x.rfind("海")
7
>>> x.index("龟")
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
x.index("龟")
ValueError: substring not found
>>> x.find("龟")
-1
>>> code = """
print("I love FishC")
print("I love my wife")"""
>>> new_code = code.expandtabs(4)
>>> print(new_code)
print("I love FishC")
print("I love my wife")
>>> "在吗!我在你家楼下,快点下来!!!".replace("在吗","想你")
SyntaxError: invalid character ',' (U+FF0C)
>>> "在吗!我在你家楼下,快点下来!!!".replace("在吗","想你")
'想你!我在你家楼下,快点下来!!!'
>>> table = str.maketrans("ABCDEFG","1234567")
>>> "I love FishC".translate(table)
'I love 6ish3'
>>> "I love FishC".translate(str.maketrans("ABCDEFG","1234567"))
'I love 6ish3'
>>> "I love FishC".translate(str.maketrans("ABCDEFG","1234567","love"))
'I6ish3'
>>>
**字符串2**
查找:count(sub[,start[,end]]) find(sub[,start[,end]]) rfind(sub[,start[,end]])
index(sub[,start[,end]]) rindex]]
count(sub[,start[,end]]):返回 sub 在字符串中不重叠的出现次数,可选参数 start 和 end 用于指定起始和结束位置
>>> x = "上海自来水来自海上"
>>> x.count("海")
2
>>> x.count("海", 0, 5)
1
find(sub[,start[,end]]):在字符串中查找 sub 子字符串,返回匹配的最低索引值;可选参数 start 和 end 用于指定起始和结束位置;如果未能匹配子字符串,返回 -1
rfind(sub[,start[,end]]):在字符串中自右向左查找 sub 子字符串,返回匹配的最高索引值;可选参数 start 和 end 用于指定起始和结束位置;如果未能匹配子字符串,返回 -1
>>> x.find("海")
1
>>> x.rfind("海")
7
index(sub[,start[,end]]) :在字符串中查找 sub 子字符串,返回匹配的最低索引值;可选参数 start 和 end 用于指定起始和结束位置;如果未能匹配子字符串,抛出 ValueError 异常
rindex]]:在字符串中自右向左查找 sub 子字符串,返回匹配的最高索引值;可选参数 start 和 end 用于指定起始和结束位置;如果未能匹配子字符串,抛出 ValueError 异常
>>> x.find("龟")
-1
>>> x.rfind("龟")
-1
>>> x.index("龟")
[报错]
>>> x.index("海")
1
>>> x.rindex("海")
7
替换:expandtabs() replace(old,new,count=-1) translate(table)
expandtabs():返回一个使用空格替换制表符的新字符串,如果没有指定 tabsize 参数,那么默认 1 个制表符 = 8 个空格
>>> code = """
print("I love FishC")[使用tab键]
print("I love my wife")"""[使用4个空格]
>>> new_code = code.expandtabs(4)
>>> print(new_code)
print("I love FishC")
print("I love my wife")
replace(old,new,count=-1):返回一个将所有 old 参数指定的子字符串替换为 new 的新字符串;count 参数指定替换的次数,默认是 -1,表示替换全部
>>> "在吗?我在你家楼下,快点下来!!".replace("在吗","想你")
'想你?我在你家楼下,快点下来!!'
translate(table):返回一个根据 table 参数转换后的新字符串;table 参数应该提供一个转换规则(可以由 str.maketrans('a', 'b') 进行定制,例如 "FishC".translate(str.maketrans("FC", "15")) -> '1ish5')
>>> table = str.maketrans("ABCDEFG","1234567")
>>> "I love FishC".translate(table)
'I love 6ish3'
>>> "I love FishC".translate(str.maketrans("ABCDEFG","1234567"))
'I love 6ish3'
str.maketrans方法还支持第三个参数,表示将指定的字符串忽略掉
>>> "I love FishC".translate(str.maketrans("ABCDEFG","1234567","love"))
'I6ish3'
{:10_265:}
……占个位
催更!
>>> x = '上海自来水来自海上'
>>> x.count('海')
2
>>> x.count('海',0,5)
SyntaxError: invalid character ',' (U+FF0C)
>>> x.count('海',0,5)
1
>>> x.find('海')
1
>>> x.rfind('海')
7
>>> x.find('龟')
-1
>>> x.index('龟')
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
x.index('龟')
ValueError: substring not found
>>> code = """
print("I love FishC")
print("I love t")"""
>>> new_code = code.expandtabs(4)
>>> print(new_code)
print("I love FishC")
print("I love t")
>>> code2 = """
print("I love FishC")
print("I love t")"""
>>> new_code2 = code2.expandtabs(3)
>>> new_code2
'\n print("I love FishC")\n print("I love t")'
>>> "在吗?我在你家楼下,快点下来!!!".replace("在吗","想你")
'想你?我在你家楼下,快点下来!!!'
>>> tab = str.maketrans("ABCDEFG","1234567")
>>> "I love FishC".translate(tab)
'I love 6ish3'
>>> "I love FishC".translate(str.maketrans("ABCDEFG","1234567"))
'I love 6ish3'
>>> "I love FishC".translate(str.maketrans("ABCDEFG","1234567","love"))
'I6ish3'
>>>
为什么我的expandtabs用不了啊,有没有大佬可以帮忙解释一下谢谢了,3.10.0版本{:10_266:}
√
打卡~~{:10_254:}{:10_254:}
打卡
是哪位鱼油皮的“明天到操场”{:10_274:}
打卡
不努力学习永远不会虽然学了也不一定会
打卡
sofa
>>> a='连裤袜丝袜长筒袜'
>>> a.count(0,6)
Traceback (most recent call last):
File "<pyshell#332>", line 1, in <module>
a.count(0,6)
TypeError: must be str, not int
>>> a.count('袜',0,6)
2
>>> a='丝袜短袜小腿袜'
>>> a.count('袜',0,6)
2
>>> a.count('袜',0,7)
3
>>> a.find('袜')
1
>>> a.rfind('袜')
6
>>> a.find('1')
-1
>>> a.rfind('1')
-1
>>> a.index('1')
Traceback (most recent call last):
File "<pyshell#341>", line 1, in <module>
a.index('1')
ValueError: substring not found
>>> code='11111'
>>> code='11111'
>>> code='11111' print('22222')
SyntaxError: invalid syntax
>>> '111'.replace('111','2222')
'2222'
>>> t=str.maketrans('123456'='wxhnjz')
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?
>>> t=str.maketrans('123456','wxhnjz')
>>> I love 123456.translate(t)
SyntaxError: invalid syntax
>>> 'I love 123456'.translate(t)
'I love wxhnjz'
>>> 'I believe 123456'.translate(t)
'I believe wxhnjz'
>>> 'I believe 123456'.translate(str.maketrans('123456','wxhnjz'))
'I believe wxhnjz'
>>> 'I believe 123456'.translate(str.maketrans('123456','wxhnjz'),'believe')
Traceback (most recent call last):
File "<pyshell#360>", line 1, in <module>
'I believe 123456'.translate(str.maketrans('123456','wxhnjz'),'believe')
TypeError: translate() takes exactly one argument (2 given)
>>> 'I believe 123456'.translate(str.maketrans('123456','wxhnjz','believe'))
'Iwxhnjz'
>>> 'I believe 123456'.translate(str.maketrans('123456','wxhnjz','I believe'))
'wxhnjz'
>>> t
{49: 119, 50: 120, 51: 104, 52: 110, 53: 106, 54: 122}
>>>t=str.maketrans('123456','wxhnjz')
SyntaxError: unexpected indent
>>> t=str.maketrans('123456','wxhnjz')
>>> 'I believe 123456'.translate(t)
'I believe wxhnjz'
>>> 'I believe 123456'.translate(t)
'I believe wxhnjz'
>>> t=str.maketrans('123456','wxhnjz','I believe')
>>> 'I believe 123456'.translate(t)
'wxhnjz'
>>> x='我永远喜欢丝袜连裤袜小腿袜长筒袜连身袜全包丝袜'
>>> x.count('袜')
6
>>> x
'我永远喜欢丝袜连裤袜小腿袜长筒袜连身袜全包丝袜'
>>> x.index('袜')
6
>>> x.rindex('袜')
22
>>> x.find('袜')
6
>>> x.rfind('袜')
22
已有 25 人购买 本主题需向作者支付 5 鱼币 才能浏览