title: python colored text without modifying the string
body: <p>I have a python string:</p>
  
  <pre><code>my_string = 'thisisjustadummystringfortestingpurposes'
  </code></pre>
  
  <p>I want to print some regions of the string with color red, so I use ANSI escape codes:</p>
  
  <ul>
  <li>red: '\x1b[31m'</li>
  <li>black: '\x1b[0m'</li>
  </ul>
  
  <p>Then what I do is, I create a new string with the ANSI codes inserted.</p>
  
  <p>Example:</p>
  
  <pre><code>my_color_seq = seq[:5]       # 0:5 will be black
  my_color_seq += '\x1b[31m'   # start red
  my_color_seq += seq[5:10]    # 5:10 will be red
  my_color_seq += '\x1b[0m'    # start black
  my_color_seq += seq[10:]     # 10: will be black
  
  print(my_color_seq)
  </code></pre>
  
  <p>This code works and correctly prints the region 5:10 red.
  I have even created a loop where I can use a list of indexes and lengths for the regions that I want colored with red.</p>
  
  <p>Now the problem is that in my colored string contain non visible characters (the ANSI codes), so the index of the characters is no longer what it seems to be.</p>
  
  <p>Imagine that I want to now process again the colored string by painting some other fragments in blue. I can no longer trust the indexes since my string is now larger than it seems.</p>
  
  <p>Of course I can update the indexes to point the correct locations in the new string, but I was just wondering if there is an easier way of doing this.</p>