1

If string are lists, I'm not understanding how the code counts the list. More specifically, i counts from 0,1,2,... but returns a string?

I've tried using print(word[i]), print(i), print(int(i))

word = input('\n')

for i in word:

    if i.isupper() == True:

        print(i)

If the input is 'STrinG', the expected result is [0,1,5] but actual results is S T G

1 Answers1

1

Your for loop iterates through the string, not through the indices. i takes on the values in "STrinG" in order, one letter at a time. If you want the indices, you need to iterate through that, instead:

for i in range(len(word)):
    if word[i].isupper():
        print(i)

Better yet, use enumerate to do both:

for i, char in enumerate(word):
    if char.isupper():
        print(i)

Does that get you moving?

Prune
  • 72,213
  • 14
  • 48
  • 72