-1

Please explain how Python evaluates this string so that one word can be set with a value greater than another word in the string? How is b > n.

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
eebbesen
  • 5,070
  • 8
  • 48
  • 70
scopa
  • 513
  • 2
  • 8
  • 17
  • find returns the position so you're really comparing numbers. http://docs.python.org/2/library/string.html#string.find – sachleen Feb 11 '14 at 19:22
  • See [this question](http://stackoverflow.com/questions/4806911/string-comparison-technique-used-by-python) – eebbesen Feb 11 '14 at 19:38
  • Try calling your function with _'The sax player hit a wrong note when the band played the lambada'_. – pillmuncher Feb 11 '14 at 20:09

1 Answers1

1

b and n are integers. The str.find() method returns the position in str where the argument was found, or -1 if the argument was not found.

So, any string s containing both not and bad and bad follows not will do:

>>> 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"
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thank you - I understand - this was very helpful as an exercise to test right in interpreter. Appreciate your help – scopa Feb 11 '14 at 19:46