1
text = "iiiiiiWiiiiiiWWiiiiW"
for char in text:
    if (char == "W"):
        z = text.index(char)
        print z

I'm having a problem with the above code. I'm not getting my desired response. I am receiving:

>>>9
>>>9
>>>9
>>>9

...instead of getting something like...

>>>9
>>>16
>>>17
>>>22

This is confusing me :( please help and thanks :)

2
  • I tried different variations of code and it seems as if there is no changing the z variable. Commented May 19, 2016 at 1:15
  • It always helps to read the docs for built-in methods: docs.python.org/2/library/string.html#string.index Commented May 19, 2016 at 1:21

1 Answer 1

1

text.index('W') will always return the position of the first occurence of 'W' in text. You could do it like that:

text = "iiiiiiWiiiiiiWWiiiiW"
for pos, char in enumerate(text):
    if char == "W":
        print pos
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.