There's a builtin method find on string objects.

s = "Happy Birthday"
s2 = "py"

print(s.find(s2))

Python is a "batteries included language" there's code written to do most of what you want already (whatever you want).. unless this is homework :)

find returns -1 if the string cannot be found.

Answer from demented hedgehog on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ find-the-index-of-a-substring-in-python
Find the Index of a Substring in Python - GeeksforGeeks
July 23, 2025 - s = "Geeks for Geeks" # Define ... substring '{s1}' is not present in the text.") ... The substring 'for' is found at index 6. The re.search() in Python's re module can be used to locate the first occurrence of a pattern ...
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ string โ€บ index
Python String index()
The index() method returns the index of a substring inside the string (if found).
๐ŸŒ
Built In
builtin.com โ€บ software-engineering-perspectives โ€บ python-substring-indexof
5 Ways to Find the Index of a Substring in Python | Built In
Below is a summary of what you need to remember about each Python string method. str.find(), str,index(): These return the lowest index of the substring.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ how-to-index-and-slice-strings-in-python-3
How To Index and Slice Strings in Python | DigitalOcean
September 29, 2025 - Key Differences: The distinction between indexing (which returns a single character and raises an IndexError if out of bounds) and slicing (which returns a substring and gracefully handles out-of-bounds requests by returning an empty string). String Reversal: The efficient and Pythonic way to reverse a string using the [::-1] slice.
๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ python string index
How to find the position of a substring with Python string index
January 23, 2024 - Python string index lets you find the position of a substring in a string. Hereโ€™s how the method works and what it offers.
๐ŸŒ
Medium
medium.com โ€บ better-programming โ€บ 5-ways-to-find-the-index-of-a-substring-in-python-13d5293fc76d
5 Ways to Find the Index of a Substring in Python | by Indhumathy Chelliah | Better Programming
September 2, 2021 - str.find() returns the lowest index in the string where the substring sub is found within the slice s[start:end]. It returns -1 if the sub is not found. start and end are optional arguments. -python docs
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ standard-library โ€บ str โ€บ index
Python str index() - Find Substring Index | Vultr Docs
November 26, 2024 - The index() method in Python is a useful string method for finding the starting index of a substring within a string. This method raises an exception if the substring is not found, making it distinguishable from similar methods like find(), ...
Find elsewhere
๐ŸŒ
datagy
datagy.io โ€บ home โ€บ python posts โ€บ python strings โ€บ python: find an index (or all) of a substring in a string
Python: Find an Index (or all) of a Substring in a String โ€ข datagy
December 20, 2022 - Letโ€™s see how we can use the str.rindex() method to find the last index of a substring in Python: a_string = "the quick brown fox jumps over the lazy dog.
๐ŸŒ
Python Tutorial
pythontutorial.net โ€บ home โ€บ python string methods โ€บ python string index()
Python String index(): Locating the Index of a Substring in a String
December 28, 2020 - In this tutorial, you'll learn how to use the Python string index() method to get the lowest index of a substring in a string.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-string-substring
Python String Substring | DigitalOcean
August 4, 2022 - def find_all_indexes(input_str, substring): l2 = [] length = len(input_str) index = 0 while index < length: i = input_str.find(substring, index) if i == -1: return l2 l2.append(i) index = i + 1 return l2 s = 'This Is The Best Theorem' print(find_all_indexes(s, 'Th')) ... You can checkout complete python script and more Python examples from our GitHub Repository.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ python โ€บ string_index.htm
Python String index() Method
The python string index() method is used to return the index of the input string where the substring is found. Basically, it helps us to find out if the specified substring is present in the input string.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-string-index-method
Python String index() Method - GeeksforGeeks
May 2, 2025 - The index() method in Python is used to find the position of a specified substring within a given string. It is similar to the find() method but raises a ValueError if the substring is not found, while find() returns -1. This can be helpful ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_index.asp
Python String index() Method
Remove List Duplicates Reverse ... Python Interview Q&A Python Bootcamp Python Training ... The index() method finds the first occurrence of the specified value....
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ practicing-string-operations-and-type-conversions-in-python โ€บ lessons โ€บ exploring-substring-search-in-python-strings
Exploring Substring Search in Python Strings
We use the built-in zip() function to create pairs of original strings and substrings. We then use the find() method to find the first occurrence of each substring in its related original string. Python string objects have a built-in method called str.find(substring, starting_index=0), which comes in handy here.
๐ŸŒ
Tutorial Teacher
tutorialsteacher.com โ€บ python โ€บ string-index
Python string.index() Method (With Examples)
The index() method returns the index of the first occurence of a substring in the given string. It is same as the find() method except that if a substring is not found, then it raises an exception.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ how-to-substring-a-string-in-python
How to Substring a String in Python
January 3, 2020 - This is often called "slicing". Here is the syntax: string[start:end:step] Where, start: The starting index of the substring. The character at this index is included in the substring.
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ extract a substring from a string in python
Extract a substring from a string in Python
2 weeks ago - Extract substrings from Python strings using slice notation with [start:end] indexes, or use re.search() with regex patterns for matching specific formats
๐ŸŒ
LearnDataSci
learndatasci.com โ€บ solutions โ€บ python-substring
Quickly Substring a String in Python โ€“ LearnDataSci
By defining the start and end points for the slice, we get the substring example_substring. This substring begins at the example_string index which matches the start point. In our case, this is index 0. The substring ends at the index that comes before the end point.