title: Replace space-char by newline-char within max-line-length in python
body: <p>I am trying to write a python script, 
  which breaks a continuous string into lines, 
  when the <code>max_line_length</code> has been exceeded. </p>
  
  <p>It shall not break words, 
  and searches therefore the last occurrence of a whitespace-char,
  which will be replaced by a newline-char.</p>
  
  <p>For some reason it does not break within the specified limit.<br>
  E.g. when defining the <code>max_line_length = 80</code>, 
  the text sometimes breaks at 82 or 83, etc.</p>
  
  <p>Since quite some time I am trying to fix the problem, 
  however it feels like i am having the tunnel vision 
  and don't see the problem here:</p>
  
  <pre><code>#!/usr/bin/python
  import sys
  
  if len(sys.argv) &lt; 3:
      print('usage:   $ python3 breaktext.py &lt;max_line_length&gt; &lt;file&gt;')
      print('example: $ python3 breaktext.py 80 infile.txt')
      exit()
  
  filename = str(sys.argv[2])
  with open(filename, 'r') as file:
      text_str = file.read().replace('\n', '')
  
      m = int(sys.argv[1])        # max_line_length
      text_list = list(text_str)  # convert string to list
      l = 0;                      # line_number
      i = m+1                     # line_character_index
      index = m+1                 # total_list_index
      while index &lt; len(text_list):
          while text_list[l * m + i] != ' ':
              i -= 1
              pass
          text_list[l * m + i] = '\n'
          l += 1
          i = m+1
          index += m+1
          pass
  
      text_str = ''.join(text_list)
      print(text_str)
  </code></pre>