René's URL Explorer Experiment


Title: Use .index() to Get the Location of the Substring (Video) – Real Python

Open Graph Title: Use .index() to Get the Location of the Substring – Real Python

Description: So I’ve quickly mentioned at the end of the last lesson that the substring that Python finds when you just do a substring check using the in operator is the first one, so the first occurrence of the substring in your string. Now maybe you want to…

Open Graph Description: So I’ve quickly mentioned at the end of the last lesson that the substring that Python finds when you just do a substring check using the in operator is the first one, so the first occurrence of the substring in your string. Now maybe you want to…

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

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Use .index() to Get the Location of the Substring",
    "description": "So I’ve quickly mentioned at the end of the last lesson that the substring that Python finds when you just do a substring check using the in operator is the first one, so the first occurrence of the substring in your string. Now maybe you want to…",
    "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": "PT2M35S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/index-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/index-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/index-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%2Findex-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/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
Transcripthttps://realpython.com/lessons/index-python-substring/#transcript
Discussionhttps://realpython.com/lessons/index-python-substring/#discussion
00:00https://realpython.com/lessons/index-python-substring/#t=0.67
So I’ve quickly mentioned at the end of the last lesson that the substring thathttps://realpython.com/lessons/index-python-substring/#t=0.67
Python finds when you just do a substring check using the in operator is thehttps://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:13https://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 thehttps://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:25https://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 argumenthttps://realpython.com/lessons/index-python-substring/#t=30.96
the substring, and then Python will return to you the indexhttps://realpython.com/lessons/index-python-substring/#t=32.94
position of the first letter of the first occurrence of thehttps://realpython.com/lessons/index-python-substring/#t=37.82
substring in your string.https://realpython.com/lessons/index-python-substring/#t=42.5
00:43https://realpython.com/lessons/index-python-substring/#t=43.87
So here you get position 59, and that means that here at positionhttps://realpython.com/lessons/index-python-substring/#t=43.87
59 is where the first occurrence of the substring "secret" startshttps://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 thehttps://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:05https://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:15https://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:23https://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:34https://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 findhttps://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 inhttps://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:03https://realpython.com/lessons/index-python-substring/#t=123.84
So if you just want to search within a specific part of your string, thenhttps://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 stringhttps://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 forhttps://realpython.com/lessons/index-python-substring/#t=140.78
doing the substring check in Python, and I’ll show you how it works and alsohttps://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 Memberhttps://realpython.com/account/join/
https://realpython.com/lessons/count-substring-python/
Overviewhttps://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 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.