|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/count-substring-python/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/quizzes/ |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Code Mentor →BetaPersonalized code assistance & learning tools | https://realpython.com/mentor/ |
| Unlock All Content → | https://realpython.com/account/join/ |
|
More
| https://realpython.com/lessons/count-substring-python/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Tutorial Writer | https://realpython.com/write-for-us/ |
| Become a Video Instructor | https://realpython.com/become-an-instructor/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?next=%2Flessons%2Fcount-substring-python%2F |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-string-contains-substring |
| Unlock This Lesson | https://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-string-contains-substring |
| https://realpython.com/courses/python-string-contains-substring/#team |
| Check if a Python String Contains a Substring | https://realpython.com/courses/python-string-contains-substring/ |
| Martin Breuss | https://realpython.com/courses/python-string-contains-substring/#team |
| Recommended Tutorial | https://realpython.com/python-string-contains-substring/ |
| Course Slides (PDF) | https://realpython.com/courses/python-string-contains-substring/downloads/python-string-contains-substring-slides/ |
| Sample Code (ZIP) | https://realpython.com/courses/python-string-contains-substring/downloads/python-string-contains-substring-code/ |
| Ask a Question | https://realpython.com/lessons/count-substring-python/#discussion |
| https://realpython.com/feedback/survey/course/python-string-contains-substring/liked/?from=lesson-title |
| https://realpython.com/feedback/survey/course/python-string-contains-substring/disliked/?from=lesson-title |
| Transcript | https://realpython.com/lessons/count-substring-python/#transcript |
| Discussion | https://realpython.com/lessons/count-substring-python/#discussion |
| 00:00 | https://realpython.com/lessons/count-substring-python/#t=0.76 |
| So here you have the lowercased version of the text that we were working with, | https://realpython.com/lessons/count-substring-python/#t=0.76 |
| and you identified that the substring "secret" appears in this | https://realpython.com/lessons/count-substring-python/#t=4.93 |
| text. But, for example, you don’t really know how often is it in there, | https://realpython.com/lessons/count-substring-python/#t=9.87 |
| right? If you want to know that, then you can use .count(). | https://realpython.com/lessons/count-substring-python/#t=13.79 |
| 00:17 | https://realpython.com/lessons/count-substring-python/#t=17.88 |
| You can say text_lower.count() | https://realpython.com/lessons/count-substring-python/#t=17.88 |
| and then pass in the substring as an argument here. | https://realpython.com/lessons/count-substring-python/#t=21.6 |
| That would be "secret". | https://realpython.com/lessons/count-substring-python/#t=25.86 |
| 00:28 | https://realpython.com/lessons/count-substring-python/#t=28.44 |
| And then Python will return an integer that tells you how often did it find | https://realpython.com/lessons/count-substring-python/#t=28.44 |
| the substring inside of that string. | https://realpython.com/lessons/count-substring-python/#t=33.1 |
| You can count this manually here too. You can see that here’s "secret" once, | https://realpython.com/lessons/count-substring-python/#t=36.08 |
| twice, three times. And then, | https://realpython.com/lessons/count-substring-python/#t=40.7 |
| the fourth time might be a little surprising, | https://realpython.com/lessons/count-substring-python/#t=43.98 |
| but it’s down here, and it’s part of the word "secretly". Now, | https://realpython.com/lessons/count-substring-python/#t=46.04 |
| keep in mind that for Python, any character is just a character, | https://realpython.com/lessons/count-substring-python/#t=50.1 |
| so the whitespace character doesn’t really have the meaning that it has for us by | https://realpython.com/lessons/count-substring-python/#t=53.26 |
| delimiting a word. It’s just another character. | https://realpython.com/lessons/count-substring-python/#t=57.1 |
| 00:59 | https://realpython.com/lessons/count-substring-python/#t=59.18 |
| So Python looks through the string, through every character, | https://realpython.com/lessons/count-substring-python/#t=59.18 |
| including the whitespaces, and looks if this substring appears somewhere. | https://realpython.com/lessons/count-substring-python/#t=63.26 |
| And also here in the word "secretly", you have the substring "secret", | https://realpython.com/lessons/count-substring-python/#t=66.78 |
| so that becomes part of the count. Now, there are some tricks that you could do. | https://realpython.com/lessons/count-substring-python/#t=71.23 |
| 01:15 | https://realpython.com/lessons/count-substring-python/#t=75.46 |
| For example, if you really wanted just the word "secret", | https://realpython.com/lessons/count-substring-python/#t=75.46 |
| you could include the white space characters in your search, | https://realpython.com/lessons/count-substring-python/#t=77.86 |
| and then Python is only going to find it once, | https://realpython.com/lessons/count-substring-python/#t=81.28 |
| and that is over here, all right. | https://realpython.com/lessons/count-substring-python/#t=84.44 |
| 01:27 | https://realpython.com/lessons/count-substring-python/#t=87.88 |
| And you can see already that if you really wanted to find all the word "secret" | https://realpython.com/lessons/count-substring-python/#t=87.88 |
| 01:32 | https://realpython.com/lessons/count-substring-python/#t=92.8 |
| but ignore that here’s a comma, and here’s a period, | https://realpython.com/lessons/count-substring-python/#t=92.8 |
| and here’s like two whitespaces left and right, | https://realpython.com/lessons/count-substring-python/#t=96.72 |
| and then maybe exclude this one because it has some characters after it, | https://realpython.com/lessons/count-substring-python/#t=98.74 |
| that gets a bit complicated here, right? | https://realpython.com/lessons/count-substring-python/#t=103.73 |
| 01:46 | https://realpython.com/lessons/count-substring-python/#t=106.31 |
| So if you want to search with these sorts of conditions, | https://realpython.com/lessons/count-substring-python/#t=106.31 |
| then there’s a better way of doing that, | https://realpython.com/lessons/count-substring-python/#t=109.77 |
| and you’ll learn about that in a couple lessons down the road. But for now, | https://realpython.com/lessons/count-substring-python/#t=111.34 |
| .count() can give you an information of how often is the substring really | https://realpython.com/lessons/count-substring-python/#t=116.13 |
| inside of the string that you’re searching in. | https://realpython.com/lessons/count-substring-python/#t=120.94 |
| 02:04 | https://realpython.com/lessons/count-substring-python/#t=124.45 |
| Now that you know that there’s four substrings "secret" inside of the text_lower, | https://realpython.com/lessons/count-substring-python/#t=124.45 |
| you might be wondering when you typed "secret" in | https://realpython.com/lessons/count-substring-python/#t=129.69 |
| text_lower, which of those did Python really find? | https://realpython.com/lessons/count-substring-python/#t=135.23 |
| 02:20 | https://realpython.com/lessons/count-substring-python/#t=140.18 |
| The quick answer to that is that it’s the first occurrence, | https://realpython.com/lessons/count-substring-python/#t=140.18 |
| but let’s look a little closer to find out a bit more information about that in | https://realpython.com/lessons/count-substring-python/#t=143.78 |
| the next lesson. | https://realpython.com/lessons/count-substring-python/#t=148.32 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/string-methods-python-substring/ |
| Overview | https://realpython.com/courses/python-string-contains-substring/ |
| https://realpython.com/lessons/index-python-substring/ |
|
Check if a Python String Contains a Substring (Overview) 02:00
| https://realpython.com/videos/python-string-contains-substring-overview/ |
|
Confirm the Presence of a Substring 04:31
| https://realpython.com/videos/confirm-substring-python/ |
|
Generalize Your Substring Check 04:10
| https://realpython.com/videos/generalize-substring-check/ |
|
Learn More Using String Methods 01:49
| https://realpython.com/lessons/string-methods-python-substring/ |
|
Use .count() to Get the Number of Occurrences 02:30
| https://realpython.com/lessons/count-substring-python/ |
|
Use .index() to Get the Location of the Substring 02:35
| https://realpython.com/lessons/index-python-substring/ |
|
Avoid Using .find() to Check for Substrings 05:20
| https://realpython.com/lessons/avoid-find-python-substring/ |
|
Get to Know Regex for Finding Substrings 01:32
| https://realpython.com/lessons/substrings-regex-python-intro/ |
|
Match a Substring Using re.search() 03:51
| https://realpython.com/lessons/substring-re-search-python/ |
|
Explore a Match Object 03:25
| https://realpython.com/lessons/match-object-substring/ |
|
Find All Pattern Matches 03:28
| https://realpython.com/lessons/find-pattern-matches-python/ |
|
Get All Matches as Match Objects 03:26
| https://realpython.com/lessons/get-match-objects-python/ |
|
Wrap Up Finding Substrings With Regex 01:51
| https://realpython.com/lessons/substrings-regex-python-summary/ |
|
Set Up and Inspect the Data 02:10
| https://realpython.com/lessons/inspect-pandas-data/ |
|
Filter DataFrame Rows With .str.contains() 02:03
| https://realpython.com/lessons/filter-dataframe-str-contains/ |
|
Use Regular Expressions With pandas 02:23
| https://realpython.com/lessons/regular-expressions-pandas/ |
|
Check if a Python String Contains a Substring (Quiz) 11:00
| https://realpython.com/lessons/python-string-contains-substring-quiz/ |
|
Check if a Python String Contains a Substring (Summary) 02:22
| https://realpython.com/lessons/python-string-contains-substring-summary/ |
| Privacy Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover