title: Python - Remove white space using only loops
body: <p>I want to remove extra spaces in a string using only <code>for/while</code> loops, and <code>if</code> statements; NO split/replace/join.    </p>
  
  <p>like this:</p>
  
  <pre><code>mystring = 'Here is  some   text   I      wrote   '
  
  while '  ' in mystring:
      mystring = mystring.replace('  ', ' ')
  
  print(mystring)
  </code></pre>
  
  <p><strong>output:</strong> </p>
  
  <pre><code>Here is some text I wrote
  </code></pre>
  
  <p>Here's what I tried.  Unfortunately, it doesn't quite work.</p>
  
  <pre><code>def cleanupstring(S):
      lasti = ""
      result = ""
  
      for i in S:
          if lasti == " " and i == " ":
              i = ""
  
          lasti = i    
          result += i    
  
      print(result)
  
  
  cleanupstring("Hello      my name    is    joe")
  </code></pre>
  
  <p><strong>output:</strong> </p>
  
  <pre><code>Hello   my name  is  joe
  </code></pre>
  
  <p>My attempt doesn't remove all the extra spaces.</p>