Python字符串和排序?

时间:2014-02-11 19:19:07

标签: python string

请解释Python如何评估此字符串,以便可以设置一个字的值大于字符串中的另一个字? b怎么样? ñ。

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

1 个答案:

答案 0 :(得分:1)

bn整数str.find() method返回找到参数的str中的位置,如果找不到参数,则返回-1

因此,s后面包含notbad 以及 bad的任何字符串not都可以:

>>> s = "That's not bad at all"
>>> n = s.find('not')
>>> b = s.find('bad')
>>> n
7
>>> b
11
>>> b > n
True
>>> s[:n] + 'good' + s[b+3:]
"That's good at all"