René's URL Explorer Experiment


Title: Avoid Using .find() to Check for Substrings (Video) – Real Python

Open Graph Title: Avoid Using .find() to Check for Substrings – Real Python

Description: In the previous lesson, I showed you that you can use text_lower.index() to get the index position of the first occurrence of the substring. This is where it starts. And there is a second string method called .find() that does basically the same. So…

Open Graph Description: In the previous lesson, I showed you that you can use text_lower.index() to get the index position of the first occurrence of the substring. This is where it starts. And there is a second string method called .find() that does basically the same. So…

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

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Avoid Using .find() to Check for Substrings",
    "description": "In the previous lesson, I showed you that you can use text_lower.index() to get the index position of the first occurrence of the substring. This is where it starts. And there is a second string method called .find() that does basically the same. So…",
    "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": "PT5M20S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/avoid-find-python-substring/#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/avoid-find-python-substring/
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/avoid-find-python-substring/
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%2Favoid-find-python-substring%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/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
Transcripthttps://realpython.com/lessons/avoid-find-python-substring/#transcript
Discussionhttps://realpython.com/lessons/avoid-find-python-substring/#discussion
00:00https://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:10https://realpython.com/lessons/avoid-find-python-substring/#t=10.53
This is where it starts. And there is a second string method calledhttps://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:25https://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 Pythonhttps://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:42https://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:50https://realpython.com/lessons/avoid-find-python-substring/#t=50.36
I prefer to use .index() because I have the feeling it’s a little morehttps://realpython.com/lessons/avoid-find-python-substring/#t=50.36
descriptive of what do both of these methods actually return because they returnhttps://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:06https://realpython.com/lessons/avoid-find-python-substring/#t=66.21
So if I say text_lower.index() and pass it ahttps://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:15https://realpython.com/lessons/avoid-find-python-substring/#t=75.52
then .index() gives a ValueError and just tells me that the substring isn’thttps://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. Ifhttps://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:29https://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:38https://realpython.com/lessons/avoid-find-python-substring/#t=98.02
Instead it returns the value -1, and thishttps://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, buthttps://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:52https://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 thanhttps://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 thehttps://realpython.com/lessons/avoid-find-python-substring/#t=118.89
.find() string method and using it for finding a substring in a string, and thenhttps://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:18https://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 likehttps://realpython.com/lessons/avoid-find-python-substring/#t=144.69
02:31https://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 elsehttps://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 constructshttps://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 -1https://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:04https://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 becausehttps://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:20https://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:35https://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:53https://realpython.com/lessons/avoid-find-python-substring/#t=233.47
and elsehttps://realpython.com/lessons/avoid-find-python-substring/#t=233.47
03:56https://realpython.com/lessons/avoid-find-python-substring/#t=236.12
I want to print("substring not in string") just to remake the same structurehttps://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 inhttps://realpython.com/lessons/avoid-find-python-substring/#t=244.11
operator than if you use .find() and then compare to the -1https://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 thathttps://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:29https://realpython.com/lessons/avoid-find-python-substring/#t=269.76
Well, you shouldn’t be using that, and instead just stick with usinghttps://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:42https://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 tohttps://realpython.com/lessons/avoid-find-python-substring/#t=300.76
deal with the return value of -1. And which of those two you wanted tohttps://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:12https://realpython.com/lessons/avoid-find-python-substring/#t=312.94
All of these string methods are useful if you want to learn more about thehttps://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 Memberhttps://realpython.com/account/join/
https://realpython.com/lessons/index-python-substring/
Overviewhttps://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 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.