|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/string-methods-python-substring/ |
| 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/string-methods-python-substring/ |
| 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%2Fstring-methods-python-substring%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/string-methods-python-substring/#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/string-methods-python-substring/#transcript |
| Discussion | https://realpython.com/lessons/string-methods-python-substring/#discussion |
| 00:00 | https://realpython.com/lessons/string-methods-python-substring/#t=0.56 |
| In this part of the course, | https://realpython.com/lessons/string-methods-python-substring/#t=0.56 |
| you’ll learn more about a couple of Python string methods that you can use to | https://realpython.com/lessons/string-methods-python-substring/#t=1.75 |
| learn more about the substring. | https://realpython.com/lessons/string-methods-python-substring/#t=6.15 |
| I’ll start off by showing you a little table that gives you an overview about | https://realpython.com/lessons/string-methods-python-substring/#t=8.1 |
| what’s the name of the string method, what happens if it does find a substring, | https://realpython.com/lessons/string-methods-python-substring/#t=11.83 |
| and what happens if it doesn’t find the substring. One that you’re going to look at | https://realpython.com/lessons/string-methods-python-substring/#t=15.99 |
| is str.count(), | https://realpython.com/lessons/string-methods-python-substring/#t=20.56 |
| and this is a way to get a count of how often does a substring appear in a | https://realpython.com/lessons/string-methods-python-substring/#t=22.33 |
| string. So if it finds the substring, | https://realpython.com/lessons/string-methods-python-substring/#t=26.79 |
| it gives you back an integer that tells you how often it is found inside of the | https://realpython.com/lessons/string-methods-python-substring/#t=29.55 |
| string. And if it’s never found, then it returns the integer 0. | https://realpython.com/lessons/string-methods-python-substring/#t=33.35 |
| 00:39 | https://realpython.com/lessons/string-methods-python-substring/#t=39.12 |
| Then there’s str.index(). | https://realpython.com/lessons/string-methods-python-substring/#t=39.12 |
| That’s a way for you to locate the substring in the string. And what it does, | https://realpython.com/lessons/string-methods-python-substring/#t=40.73 |
| it gives you back the start index of where the first occurrence of the substring | https://realpython.com/lessons/string-methods-python-substring/#t=45.49 |
| is found inside of the string that you’re searching in. | https://realpython.com/lessons/string-methods-python-substring/#t=50.18 |
| 00:53 | https://realpython.com/lessons/string-methods-python-substring/#t=53.4 |
| What it does when there is no substring in the string is to raise a ValueError. | https://realpython.com/lessons/string-methods-python-substring/#t=53.4 |
| 00:59 | https://realpython.com/lessons/string-methods-python-substring/#t=59.52 |
| And then there’s also str.find() that does the same thing as str.index() | https://realpython.com/lessons/string-methods-python-substring/#t=59.52 |
| in the success condition. So if it finds the substring, | https://realpython.com/lessons/string-methods-python-substring/#t=63.99 |
| then it gives you back the starting index position. | https://realpython.com/lessons/string-methods-python-substring/#t=66.73 |
| 01:10 | https://realpython.com/lessons/string-methods-python-substring/#t=70.0 |
| But if the substring isn’t found, then instead of raising a ValueError, | https://realpython.com/lessons/string-methods-python-substring/#t=70.0 |
| it returns -1. | https://realpython.com/lessons/string-methods-python-substring/#t=73.79 |
| I’ll tell you more about str.index() and str.find() throughout this course. | https://realpython.com/lessons/string-methods-python-substring/#t=76.17 |
| 01:20 | https://realpython.com/lessons/string-methods-python-substring/#t=80.28 |
| You may have seen str.find() used to check whether a substring is in the | https://realpython.com/lessons/string-methods-python-substring/#t=80.28 |
| string. I don’t consider this a good way to do it, | https://realpython.com/lessons/string-methods-python-substring/#t=84.78 |
| but I’ll show you how to do it later on in the course so that you also can maybe | https://realpython.com/lessons/string-methods-python-substring/#t=87.74 |
| see for yourself how it works and why using the in operator is a better choice. | https://realpython.com/lessons/string-methods-python-substring/#t=92.22 |
| 01:37 | https://realpython.com/lessons/string-methods-python-substring/#t=97.52 |
| But all of these string methods have their use cases. | https://realpython.com/lessons/string-methods-python-substring/#t=97.52 |
| They are useful for learning more about the substring. | https://realpython.com/lessons/string-methods-python-substring/#t=100.61 |
| And now let’s head back over to the REPL to try them out and learn a little more | https://realpython.com/lessons/string-methods-python-substring/#t=103.44 |
| about your substring. | https://realpython.com/lessons/string-methods-python-substring/#t=107.18 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/videos/generalize-substring-check/ |
| Overview | https://realpython.com/courses/python-string-contains-substring/ |
| https://realpython.com/lessons/count-substring-python/ |
|
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