Return the number of (non-overlapping) occurrences of substring sub in string s[start:end]. Defaults for start and end and interpretation of negative values are the same as for slices.
Return the number of (non-overlapping) occurrences of substring sub in string s[start:end]. Defaults for start and end and interpretation of negative values are the same as for slices.
来源:https://docs.python.org/2/library/string.html
试着想象一下:
在“ababa”这个词中,你看到多少个不重叠的“aba”字?我看到2。我也看到一个“c”。
在这个词中:“www”你看到多少个不重叠的“ww”字?我明白了。我也看到一个“w”。
为了得到更好的解释,当您看到实例时,可以认为您正在删除它。
对于“abacaba”,您可以看到“aba”并将其删除。现在有了“caba”,你再次看到“aba”并删除它。现在你只得到“c”。你看了两遍“aba”。对于“www”也是一样的,你只需看到一次“ww”就可以删除它。现在你只看到“w”。你只看过一次“ww”。
这是有道理的。
阅读docs:
因为“w w”是第一个匹配的,所以它从第三个“w”开始,但无法匹配“ww”。
相关问题 更多 >
编程相关推荐