我刚开始学习Python并且一直关注Google Python类.在其中一个字符串练习中,有以下代码:
def not_bad(s):
n = s.find('not')
b = s.find('bad')
if n != -1 and b != -1 and b > n:
s = s[:n] + 'good' + s[b+3:]
return s
Run Code Online (Sandbox Code Playgroud)
我想知道s [b + 3:]代表什么,因为这是我第一次遇到字符串切片中的+.
+只是加法运算符,它增加了bwith 的值3.在这种情况下使用它来跳过三个字符bad.
s[:n]保留所有角色,直到not,+ 'good' +之后的s[b+3:]所有角色bad.