mercredi 18 juillet 2018

I want to print letters(strings) with odd or even numbers as their index and save them as a list

I want to print and save letters of this word "complexity"; I want to iterate through the word and get every letter that has an even number as its index, store or save it in a list, and also get every letter that has an odd number as its index and save it as a list in their respective variables. Below is the code.

word = "complexity"
even_letters = ""
odd_letters = ""
for index in range(0,len(word)):

    if int(index) % 2 == 0:
       even_letters = word[index]
   else:
        odd_letters = word[index]

print(list(even_letters))
print(list(odd_letters))

But my results shows only this:

['t']

['y']

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire