Python - Remove whitespaces and punctuation without functions
Question: first of all, sorry for my bad english. I'm a beginner programmer and I have
some problems with my python program. I have to make a program that normalizes
the whitespaces and the punctuation, for example:

If I put a string called

    
    
    "   hello   how,   are  u?   "
    

The new string has to be...

    
    
    "Hello how are u"
    

But in my code, the result appears like this and I dont know why:

    
    
     "helloo how,, aree u??"
    

**Note:** I can't use any kind of function like split(), strip(), etc...

Here is my code:

    
    
    from string import punctuation
    
    print("Introduce your string: ")
    string = input() + " "
    word = ""
    new_word = ""
    final_string = ""
    
    #This is the main code for the program
    for i in range(0, len(string)):
        if (string[i] != " " and (string[i+1] != " " or string[i+1] != punctuation)):
            word += string[i]
        if (string[i] != " " and (string[i+1] == " " or string[i+1] == punctuation)):
            word += string[i] + " "
            new_word += word
            word = ""
    
    #This destroys the last whitespace
    for j in range(0,len(new_word)-1):
        final_string += new_word[j]
    
    print(final_string)
    

Thank you all.

**EDIT:**

Now i have this code:

    
    
    letter = False
    
    for element in my_string:
        if (element != " " and element != punctuation):
            letter= True
            word += element
    
    
    print(word)
    

But now, the problem is that my program doesn't recognize the punctuation so
if i put:

    
    
    "Hello   ... how  are u?"
    

It has to be like `"Hellohowareu"`

But it is like:

    
    
    "Hello...howareu?
    


Answer: I'm not going to write the code for you since this is obviously homework, but
I will give you some hints.

I think your approach of checking the next character is a bit error-prone.
Rather, I would have a flag that you set when you see a space or punctuation.
The next time through the loop, check if the flag is set: if it is, and you
still see a space, then ignore it, otherwise, reset the flag to false.

