python String out of index :         def cleanupstring (S):
    newstring = ["", 0]
    j = 1
    for i in range(len(S)):
        if S[i] != " " and S[i+1] != " ":
            newstring[0] = newstring[0] + S[i]
        else:
            newstring[1] = newstring [1] + 1

    return newstring


    # main program

    sentence = input("Enter a string: ")

    outputList = cleanupstring(sentence)

    print("A total of", outputList[1], "characters have been removed from your 
    string.")
    print("The new string is:", outputList[0]) 

Use Python to create a function cleanstring(S) to "clean up" the spaces in a sentence
S. The sentence may have extra spaces at the front and/or at the end and/or between
words. The subroutine returns a new version of the sentence without the extra spaces.
That is, in the new string, the words should be the same but there should be no spaces
at the start, only one space between each word and no spaces at the end.
This program is about you writing code to search through a string to find words and so
you are not allowed to use the split function in Python. You can solve this problem
with the basic capabilities of the if and while statements and string operations of len
and concatentation.
For example: if the input is: " Hello to the world !"
then the output should be: "Hello to the world!" 

Can anyone help me why my program creates an error and how to fix it