|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/index-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/index-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%2Findex-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/index-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/index-python-substring/#transcript |
| Discussion | https://realpython.com/lessons/index-python-substring/#discussion |
| 00:00 | https://realpython.com/lessons/index-python-substring/#t=0.67 |
| So I’ve quickly mentioned at the end of the last lesson that the substring that | https://realpython.com/lessons/index-python-substring/#t=0.67 |
| Python finds when you just do a substring check using the in operator is the | https://realpython.com/lessons/index-python-substring/#t=4.98 |
| first one, so the first occurrence of the substring in your string. | https://realpython.com/lessons/index-python-substring/#t=9.9 |
| 00:13 | https://realpython.com/lessons/index-python-substring/#t=13.48 |
| Now maybe you want to know where in the string does it appear. | https://realpython.com/lessons/index-python-substring/#t=13.48 |
| And for that there is a handy string method that you can use to find the | https://realpython.com/lessons/index-python-substring/#t=17.32 |
| index position of that first occurrence of the substring. | https://realpython.com/lessons/index-python-substring/#t=22.06 |
| 00:25 | https://realpython.com/lessons/index-python-substring/#t=25.99 |
| It is aptly named .index(). So you can say text_lower.index() | https://realpython.com/lessons/index-python-substring/#t=25.99 |
| and then pass it as an argument | https://realpython.com/lessons/index-python-substring/#t=30.96 |
| the substring, and then Python will return to you the index | https://realpython.com/lessons/index-python-substring/#t=32.94 |
| position of the first letter of the first occurrence of the | https://realpython.com/lessons/index-python-substring/#t=37.82 |
| substring in your string. | https://realpython.com/lessons/index-python-substring/#t=42.5 |
| 00:43 | https://realpython.com/lessons/index-python-substring/#t=43.87 |
| So here you get position 59, and that means that here at position | https://realpython.com/lessons/index-python-substring/#t=43.87 |
| 59 is where the first occurrence of the substring "secret" starts | https://realpython.com/lessons/index-python-substring/#t=48.22 |
| inside of this text up here. Okay, | https://realpython.com/lessons/index-python-substring/#t=52.97 |
| so .index() gives you a way to get a bit more information about where’s the | https://realpython.com/lessons/index-python-substring/#t=56.82 |
| substring located. It also takes optional parameters. | https://realpython.com/lessons/index-python-substring/#t=61.26 |
| 01:05 | https://realpython.com/lessons/index-python-substring/#t=65.3 |
| You can give a start index and an end index, | https://realpython.com/lessons/index-python-substring/#t=65.3 |
| so where in the string you want to search for. So this always, | https://realpython.com/lessons/index-python-substring/#t=67.91 |
| if you start at the beginning, which is the default, | https://realpython.com/lessons/index-python-substring/#t=71.99 |
| you’re always going to find the first occurrence. | https://realpython.com/lessons/index-python-substring/#t=73.98 |
| 01:15 | https://realpython.com/lessons/index-python-substring/#t=75.9 |
| But if you want to start somewhere after 59, | https://realpython.com/lessons/index-python-substring/#t=75.9 |
| then it’s going to give you the beginning position of the next one. | https://realpython.com/lessons/index-python-substring/#t=78.85 |
| 01:23 | https://realpython.com/lessons/index-python-substring/#t=83.51 |
| Let’s give it a quick spin just so that you see how that works. | https://realpython.com/lessons/index-python-substring/#t=83.51 |
| You can say text_lower.index(), then pass it "secret", | https://realpython.com/lessons/index-python-substring/#t=86.75 |
| and then here I’m going to give it a different start position. | https://realpython.com/lessons/index-python-substring/#t=91.97 |
| 01:34 | https://realpython.com/lessons/index-python-substring/#t=94.63 |
| I’m going to say start at 60. If I don’t define the end, | https://realpython.com/lessons/index-python-substring/#t=94.63 |
| then it’s just going to go to the end of the string. So in this case, | https://realpython.com/lessons/index-python-substring/#t=99.19 |
| it’s going to start searching here for the string "secret" and "ecret" | https://realpython.com/lessons/index-python-substring/#t=102.63 |
| is not the string "secret", so the first one it’s going to find | https://realpython.com/lessons/index-python-substring/#t=107.76 |
| is just going to be a little after that, and you will get the position 66, | https://realpython.com/lessons/index-python-substring/#t=110.67 |
| which is where the second occurrence of "secret" in that case starts in | https://realpython.com/lessons/index-python-substring/#t=115.03 |
| your string. And yeah, like I mentioned, you can also define the end. | https://realpython.com/lessons/index-python-substring/#t=119.99 |
| 02:03 | https://realpython.com/lessons/index-python-substring/#t=123.84 |
| So if you just want to search within a specific part of your string, then | https://realpython.com/lessons/index-python-substring/#t=123.84 |
| it’s kind of like a slice syntax that you can pass in here. | https://realpython.com/lessons/index-python-substring/#t=128.03 |
| And this can give you additional information about where inside of your string | https://realpython.com/lessons/index-python-substring/#t=131.93 |
| is the substring located. In the next lesson, | https://realpython.com/lessons/index-python-substring/#t=136.68 |
| I’m going to show you one more string method that is often a little misused for | https://realpython.com/lessons/index-python-substring/#t=140.78 |
| doing the substring check in Python, and I’ll show you how it works and also | https://realpython.com/lessons/index-python-substring/#t=145.76 |
| why it’s not the best way of doing that check. | https://realpython.com/lessons/index-python-substring/#t=151.06 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/count-substring-python/ |
| Overview | https://realpython.com/courses/python-string-contains-substring/ |
| https://realpython.com/lessons/avoid-find-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