Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 8 Online
OpenStudy (anonymous):

Is there a way that I could index a letter in a string and have Python return an integer? I.e., if I have a string s='inn and suites', can I get n1=1, n2=3, and n3=5, referring to their position? Indexing doesn't work because it only "indexes" the first occasion of the character.

OpenStudy (e.mccormick):

Because strings are indexed, you can use a slice that starts right after the last instance of a letter. In this way, you can use a loop or recursion to find every instance of a letter.

OpenStudy (anonymous):

``` >>> >>> s='inn and suites' >>> enns = [] >>> for ndx, char in enumerate(s): if char == 'n': enns.append(ndx) >>> enns [1, 2, 5] >>> ```

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!