Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Python Data Types Index

Why doesn't alpha.index('cd') give the value of 2 AND 3

At 2:30

alpha = 'abcdef' alpha.index('cd')

You get the value of 2. Why doesn't it give the value of 2 and 3 especially since he stated you could do index with groups, but it only states one value?

What if I want both occurrences (if it's just giving me the first occurrence). Why does it even give the first one and not both, is there a important disguised reason?

1 Answer

here index is returning the first occurrence of the substring 'cd' in the string 'abcde'

Thanks. What if i want both occurences?

You are asking for the occurence of substring 'cd' and there is only one occurence of that. If you want index of both 'c' and 'd' you have to do two index call. 'abcd'.index('c') and 'abcd'.index('d')