count()
A method that returns how many times a substring appears in a string, or an item in a list, without overlaps.
In more detail
"Mississippi".count("ss") is 2. For example, "banana".count("a") is 3, and counting two x characters inside a run of three finds one match, because matches do not overlap.
Example
"banana".count("a") # 3Related terms
- string method — A method that belongs to strings, such as upper, lower, count, find, replace, split, strip, startswith or join.
- substring — A run of consecutive characters inside a longer string, such as "ssi" in "Mississippi".
Taught in
- Mad Libs Revisited: Finding The Cues — Hands-on Python: Complete Video Course & Book
- Mad Libs Revisited: The Whole Program — Hands-on Python: Complete Video Course & Book
- Functions And Exceptions Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep