title: Python tkinter - Searching for key word that is not a sub string
body: <pre class="lang-py prettyprint-override"><code>import keyword
  from tkinter import END
  
  def highlight(text):
      keywords = keyword.kwlist
      for kw in keywords:
          text.tag_remove(kw, 1.0, END)
          first = 1.0
  
          while True:
              first = text.search(
                  kw, first,
                  nocase=False,
                  stopindex=END
              )
  
              if first is None or first == &quot;&quot;:
                  break
              
              first_splitted = first.split(&quot;.&quot;)
              if len(first_splitted) == 1:
                  break
  
              last = f&quot;{first_splitted[0]}.{int(first_splitted[1]) + len(kw)}&quot;
  
              character_position_before_first = f&quot;{first_splitted[0]}.{int(first_splitted[1]) - 1}&quot;
              character_before_first = text.get(character_position_before_first)
  
              last_splitted = last.split(&quot;.&quot;)
  
              character_position_after_last = f&quot;{last_splitted[0]}.{int(last_splitted[1])}&quot;
              character_after_last = text.get(character_position_after_last)
  
              if not character_before_first.isspace() and not character_after_last.isspace():   
                  break
  
              text.tag_add(kw, first, last)
              first = last
              
          text.tag_config(
              kw,
              foreground=&quot;#aa71eb&quot;
          )
  </code></pre>
  <p>Given the following code, I'm trying to highlight key words in a text. The issue is that sub strings are being marked.</p>
  <p><strong>Example:</strong>
  <code>hello this is a test open works too lmao lol lol lol</code>
  Would mark <code>is</code> from <code>this</code> and <code>is</code></p>
  <p>I only want it to mark the second <code>is</code> as the first <code>is</code> is a sub string of <code>this</code></p>
  <p>I have no clue why the code above is not working. Help would be appreciated.</p>