|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/lessons/avoid-find-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/avoid-find-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%2Favoid-find-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/avoid-find-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/avoid-find-python-substring/#transcript |
| Discussion | https://realpython.com/lessons/avoid-find-python-substring/#discussion |
| 00:00 | https://realpython.com/lessons/avoid-find-python-substring/#t=0.62 |
| In the previous lesson, I showed you that you can use text_lower.index() | https://realpython.com/lessons/avoid-find-python-substring/#t=0.62 |
| to get the index position of the first occurrence of the substring. | https://realpython.com/lessons/avoid-find-python-substring/#t=5.49 |
| 00:10 | https://realpython.com/lessons/avoid-find-python-substring/#t=10.53 |
| This is where it starts. And there is a second string method called | https://realpython.com/lessons/avoid-find-python-substring/#t=10.53 |
| .find() that does basically the same. | https://realpython.com/lessons/avoid-find-python-substring/#t=15.45 |
| So if you use .find() and then pass it "secret", | https://realpython.com/lessons/avoid-find-python-substring/#t=19.78 |
| you will get the same result, 59. | https://realpython.com/lessons/avoid-find-python-substring/#t=23.59 |
| 00:25 | https://realpython.com/lessons/avoid-find-python-substring/#t=25.45 |
| This is where the first occurrence of the substring starts. | https://realpython.com/lessons/avoid-find-python-substring/#t=25.45 |
| But this has a name that is sometimes a little misleading for Python | https://realpython.com/lessons/avoid-find-python-substring/#t=29.91 |
| programmers because .find() seems like a good way to find a string in Python, | https://realpython.com/lessons/avoid-find-python-substring/#t=34.37 |
| right? And I would argue that it is quite descriptively named. | https://realpython.com/lessons/avoid-find-python-substring/#t=39.25 |
| 00:42 | https://realpython.com/lessons/avoid-find-python-substring/#t=42.77 |
| You want to find where is the string, and if you think of it like that, | https://realpython.com/lessons/avoid-find-python-substring/#t=42.77 |
| then .find() also makes sense. It gives you the index position, right? | https://realpython.com/lessons/avoid-find-python-substring/#t=46.01 |
| 00:50 | https://realpython.com/lessons/avoid-find-python-substring/#t=50.36 |
| I prefer to use .index() because I have the feeling it’s a little more | https://realpython.com/lessons/avoid-find-python-substring/#t=50.36 |
| descriptive of what do both of these methods actually return because they return | https://realpython.com/lessons/avoid-find-python-substring/#t=53.53 |
| the index positions. Now there’s a difference between them, | https://realpython.com/lessons/avoid-find-python-substring/#t=58.33 |
| which is how they handle if they do not find the substring. | https://realpython.com/lessons/avoid-find-python-substring/#t=61.93 |
| 01:06 | https://realpython.com/lessons/avoid-find-python-substring/#t=66.21 |
| So if I say text_lower.index() and pass it a | https://realpython.com/lessons/avoid-find-python-substring/#t=66.21 |
| substring that’s not in there—let’s say "treasure"— | https://realpython.com/lessons/avoid-find-python-substring/#t=70.97 |
| 01:15 | https://realpython.com/lessons/avoid-find-python-substring/#t=75.52 |
| then .index() gives a ValueError and just tells me that the substring isn’t | https://realpython.com/lessons/avoid-find-python-substring/#t=75.52 |
| found. | https://realpython.com/lessons/avoid-find-python-substring/#t=80.41 |
| So, in my opinion, it’s quite descriptive and understandable. If | https://realpython.com/lessons/avoid-find-python-substring/#t=80.62 |
| I do the same with .find(), | https://realpython.com/lessons/avoid-find-python-substring/#t=85.52 |
| 01:29 | https://realpython.com/lessons/avoid-find-python-substring/#t=89.74 |
| it does not throw an error but instead it returns the value—no, | https://realpython.com/lessons/avoid-find-python-substring/#t=89.74 |
| well that was the wrong string. | https://realpython.com/lessons/avoid-find-python-substring/#t=95.08 |
| 01:38 | https://realpython.com/lessons/avoid-find-python-substring/#t=98.02 |
| Instead it returns the value -1, and this | https://realpython.com/lessons/avoid-find-python-substring/#t=98.02 |
| -1 in the context of .find(), | https://realpython.com/lessons/avoid-find-python-substring/#t=102.71 |
| it means the substring is not in the string, but | https://realpython.com/lessons/avoid-find-python-substring/#t=104.82 |
| it’s not really very descriptive in my opinion. | https://realpython.com/lessons/avoid-find-python-substring/#t=109.92 |
| 01:52 | https://realpython.com/lessons/avoid-find-python-substring/#t=112.95 |
| I’d rather have a ValueError thrown that tells me this is not in there than | https://realpython.com/lessons/avoid-find-python-substring/#t=112.95 |
| have -1. | https://realpython.com/lessons/avoid-find-python-substring/#t=117.88 |
| And the reason for that is that you’ll see people finding the | https://realpython.com/lessons/avoid-find-python-substring/#t=118.89 |
| .find() string method and using it for finding a substring in a string, and then | https://realpython.com/lessons/avoid-find-python-substring/#t=123.85 |
| they start writing code, a conditional where they say if text_lower.find(), | https://realpython.com/lessons/avoid-find-python-substring/#t=128.01 |
| let’s say "treasure", | https://realpython.com/lessons/avoid-find-python-substring/#t=133.07 |
| 02:18 | https://realpython.com/lessons/avoid-find-python-substring/#t=138.11 |
| != -1: | https://realpython.com/lessons/avoid-find-python-substring/#t=138.11 |
| then print("found it"), right, | https://realpython.com/lessons/avoid-find-python-substring/#t=141.74 |
| and else maybe print something like | https://realpython.com/lessons/avoid-find-python-substring/#t=144.69 |
| 02:31 | https://realpython.com/lessons/avoid-find-python-substring/#t=151.32 |
| "substring not in string". | https://realpython.com/lessons/avoid-find-python-substring/#t=151.32 |
| Of course, your conditions would probably do something else | https://realpython.com/lessons/avoid-find-python-substring/#t=154.73 |
| in here. That’s where you write your code logic. | https://realpython.com/lessons/avoid-find-python-substring/#t=157.08 |
| But what I want to show you here is that if you opt for using .find() | https://realpython.com/lessons/avoid-find-python-substring/#t=159.74 |
| in order to check for the substring in the string, | https://realpython.com/lessons/avoid-find-python-substring/#t=164.41 |
| then you come up with some kind of hard-to-read code constructs | https://realpython.com/lessons/avoid-find-python-substring/#t=167.27 |
| because in order to correctly interpret this if you’re reading code, | https://realpython.com/lessons/avoid-find-python-substring/#t=171.44 |
| you need to know that .find() returns -1 | https://realpython.com/lessons/avoid-find-python-substring/#t=174.8 |
| if it doesn’t find a substring, and that’s not entirely intuitive. | https://realpython.com/lessons/avoid-find-python-substring/#t=178.69 |
| 03:04 | https://realpython.com/lessons/avoid-find-python-substring/#t=184.53 |
| It works because we know that .find() returns -1 if it’s not found, | https://realpython.com/lessons/avoid-find-python-substring/#t=184.53 |
| we know "treasure" is not in that string, | https://realpython.com/lessons/avoid-find-python-substring/#t=189.12 |
| so this whole expression is going to evaluate to False, and because | https://realpython.com/lessons/avoid-find-python-substring/#t=191.65 |
| of that, you’re going to print out that the substring is not in the string. | https://realpython.com/lessons/avoid-find-python-substring/#t=196.6 |
| 03:20 | https://realpython.com/lessons/avoid-find-python-substring/#t=200.9 |
| But if you think back to how we did that initially, | https://realpython.com/lessons/avoid-find-python-substring/#t=200.9 |
| which is saying "treasure" in text_lower, | https://realpython.com/lessons/avoid-find-python-substring/#t=203.88 |
| this is much shorter and more concise, and also the output makes a lot of sense. | https://realpython.com/lessons/avoid-find-python-substring/#t=210.71 |
| 03:35 | https://realpython.com/lessons/avoid-find-python-substring/#t=215.24 |
| So in this case you’re just saying is it in there, and Python returns False, | https://realpython.com/lessons/avoid-find-python-substring/#t=215.24 |
| which means you can write your conditional statement like so. You can say if— | https://realpython.com/lessons/avoid-find-python-substring/#t=218.96 |
| let me just copy that—if "treasure" in text_lower: | https://realpython.com/lessons/avoid-find-python-substring/#t=223.72 |
| print("found it") | https://realpython.com/lessons/avoid-find-python-substring/#t=228.48 |
| 03:53 | https://realpython.com/lessons/avoid-find-python-substring/#t=233.47 |
| and else | https://realpython.com/lessons/avoid-find-python-substring/#t=233.47 |
| 03:56 | https://realpython.com/lessons/avoid-find-python-substring/#t=236.12 |
| I want to print("substring not in string") just to remake the same structure | https://realpython.com/lessons/avoid-find-python-substring/#t=236.12 |
| as before. So in this case, | https://realpython.com/lessons/avoid-find-python-substring/#t=240.89 |
| the first line of this code is a lot easier to interpret if you use the in | https://realpython.com/lessons/avoid-find-python-substring/#t=244.11 |
| operator than if you use .find() and then compare to the -1 | https://realpython.com/lessons/avoid-find-python-substring/#t=248.69 |
| return value. Okay, | https://realpython.com/lessons/avoid-find-python-substring/#t=253.41 |
| so this is just a little aside as to why you shouldn’t use .find() or, | https://realpython.com/lessons/avoid-find-python-substring/#t=255.65 |
| for that matter, .index() or .count() or any of these other string methods that | https://realpython.com/lessons/avoid-find-python-substring/#t=260.53 |
| you could massage in order to still basically perform a substrings check. | https://realpython.com/lessons/avoid-find-python-substring/#t=264.97 |
| 04:29 | https://realpython.com/lessons/avoid-find-python-substring/#t=269.76 |
| Well, you shouldn’t be using that, and instead just stick with using | https://realpython.com/lessons/avoid-find-python-substring/#t=269.76 |
| the in operator, | https://realpython.com/lessons/avoid-find-python-substring/#t=274.39 |
| which is the most readable way of checking for a substring in a string. | https://realpython.com/lessons/avoid-find-python-substring/#t=275.45 |
| 04:42 | https://realpython.com/lessons/avoid-find-python-substring/#t=282.56 |
| That said, all of these string methods still have their use cases. | https://realpython.com/lessons/avoid-find-python-substring/#t=282.56 |
| If you want to know how often is the substring in the string, | https://realpython.com/lessons/avoid-find-python-substring/#t=287.19 |
| you can use str.count(). If you want to know where is the substring located, | https://realpython.com/lessons/avoid-find-python-substring/#t=290.18 |
| then you can use str.index(), and you could also use str.find() | https://realpython.com/lessons/avoid-find-python-substring/#t=294.45 |
| for that same purpose, | https://realpython.com/lessons/avoid-find-python-substring/#t=299.18 |
| and that just depends on whether you want to handle an error or whether you want to | https://realpython.com/lessons/avoid-find-python-substring/#t=300.76 |
| deal with the return value of -1. And which of those two you wanted to | https://realpython.com/lessons/avoid-find-python-substring/#t=304.81 |
| use might really depend on your use case. | https://realpython.com/lessons/avoid-find-python-substring/#t=309.72 |
| 05:12 | https://realpython.com/lessons/avoid-find-python-substring/#t=312.94 |
| All of these string methods are useful if you want to learn more about the | https://realpython.com/lessons/avoid-find-python-substring/#t=312.94 |
| substring, and that’s what you should use them for. | https://realpython.com/lessons/avoid-find-python-substring/#t=316.12 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/lessons/index-python-substring/ |
| Overview | https://realpython.com/courses/python-string-contains-substring/ |
| https://realpython.com/lessons/substrings-regex-python-intro/ |
|
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