René's URL Explorer Experiment


Title: Use .count() to Get the Number of Occurrences (Video) – Real Python

Open Graph Title: Use .count() to Get the Number of Occurrences – Real Python

Description: So here you have the lowercased version of the text that we were working with, and you identified that the substring "secret" appears in this text. But, for example, you don’t really know how often is it in there, right? If you want to know that…

Open Graph Description: So here you have the lowercased version of the text that we were working with, and you identified that the substring "secret" appears in this text. But, for example, you don’t really know how often is it in there, right? If you want to know that…

Opengraph URL: https://realpython.com/lessons/count-substring-python/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Use .count() to Get the Number of Occurrences",
    "description": "So here you have the lowercased version of the text that we were working with, and you identified that the substring \u0022secret\u0022 appears in this text. But, for example, you don’t really know how often is it in there, right? If you want to know that…",
    "thumbnailUrl": ["https://files.realpython.com/media/How-to-Check-if-a-String-Contains-Another-String_Watermarked.ad5138507ec7.jpg"],
    "uploadDate": "2023-04-04T14:00:00+00:00",
    "duration": "PT2M30S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/count-substring-python/#t={seek_to_second_number}",
      "startOffset-input": "required name=seek_to_second_number"
    }
  }
  

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/How-to-Check-if-a-String-Contains-Another-String_Watermarked.ad5138507ec7.jpg
og:imagehttps://files.realpython.com/media/How-to-Check-if-a-String-Contains-Another-String_Watermarked.ad5138507ec7.jpg
twitter:creator@realpython
og:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/lessons/count-substring-python/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/lessons/count-substring-python/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Tutorial Writerhttps://realpython.com/write-for-us/
Become a Video Instructorhttps://realpython.com/become-an-instructor/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Flessons%2Fcount-substring-python%2F
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=python-string-contains-substring
Unlock This Lessonhttps://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 Substringhttps://realpython.com/courses/python-string-contains-substring/
Martin Breusshttps://realpython.com/courses/python-string-contains-substring/#team
Recommended Tutorialhttps://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 Questionhttps://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
Transcripthttps://realpython.com/lessons/count-substring-python/#transcript
Discussionhttps://realpython.com/lessons/count-substring-python/#discussion
00:00https://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 thishttps://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:17https://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:28https://realpython.com/lessons/count-substring-python/#t=28.44
And then Python will return an integer that tells you how often did it findhttps://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 byhttps://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:59https://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:15https://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:27https://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:32https://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:46https://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 reallyhttps://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:04https://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" inhttps://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:20https://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 inhttps://realpython.com/lessons/count-substring-python/#t=143.78
the next lesson.https://realpython.com/lessons/count-substring-python/#t=148.32
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/lessons/string-methods-python-substring/
Overviewhttps://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 Policyhttps://realpython.com/privacy-policy/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.