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

How do you check where in a string the specific character is?

So i know you can do something[int] to see what letter is in that spot. But say i have the string "apple", how would i check where where the p's are in the string?

1 Answer

Steven Parker
Steven Parker
243,318 Points

Strings have a "find" method. For example: "apple".find('p') would return 1, the index of the first 'p'. To find multiple cases you'd need to call it again with a start offset. See the Python Documentation for details on all the string methods.

Strings also have a "count" method if you just want to know how many it has. "apple".count('p') would return 2.