samedi 16 janvier 2021

How can i remove unwanted spaces from the string using enumerate function and for loop

i have this code but it is not working.. i don't know what's wrong in this code

djtext = "g o d  i s   gre at"
analyzed = ""
for index, char in enumerate(djtext):
    if not(djtext[index] == " " and djtext[index+1]==" "):
      analyzed = analyzed + char
print(analyzed)

I want the output like this

god is great

But it gave me this output

g o d i s gre at

Aucun commentaire:

Enregistrer un commentaire