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
🌐
Built In
builtin.com › software-engineering-perspectives › python-substring-indexof
5 Ways to Find the Index of a Substring in Python | Built In
The substring occurs two times in the string. str.find(“an”) returns the lowest index of the substring an. ... The substring is an. The start parameter is 2. It will start searching the substring an from index two.
🌐
W3Schools
w3schools.com › python › ref_string_index.asp
Python String index() Method
Remove List Duplicates Reverse ... Python Study Plan Python Interview Q&A Python Training ... The index() method finds the first occurrence of the specified value....
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-index-method
Python String index() Method - GeeksforGeeks
May 2, 2025 - Explanation: index() method searches for the substring "prog" within "Python programming" and returns starting index 7. ... end (optional): The ending index for the search. If not provided, it defaults to the length of the string.
🌐
Tutorialspoint
tutorialspoint.com › python › string_index.htm
Python String index() Method
Welcome to Tutorialspoint." str2 ... above program - ... The python string index() method returns the index where the substring is found within the range of the starting and ending index that is specified....
🌐
Programiz
programiz.com › python-programming › methods › string › index
Python String index()
Online Python Online JavaScript ... Online Go Online Rust Online Scala Online Dart Online R Online Ruby ... The index() method returns the index of a substring inside the string (if found)....
🌐
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 - You can either search the whole Python string or only a part of it. The syntax of the index method is very easy to explain. The method can be invoked with one, two, or three arguments, with the last two being optional. The first argument is always the substring, after which you can review the string.
🌐
Vultr Docs
docs.vultr.com › python › standard library › str › index()
Python str index() - Find Substring Index
November 26, 2024 - full_string = "Hello, welcome to Python." substring = "welcome" index_position = full_string.index(substring) print(index_position) ... This code finds the index of the substring "welcome" in the string "Hello, welcome to Python.".
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 - If all you want to do is first index of a substring in a Python string, you can do this easily with the str.index() method. This method comes built into Python, so there’s no need to import any packages. Let’s take a look at how you can use Python to find the first index of a substring in a string:
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-find-index-containing-string-in-list
Python - Find Index containing String in List - GeeksforGeeks
July 23, 2025 - next() method, combined with a generator expression, can be used to find the index of a string in a list.
🌐
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 = "GeeksforGeeks" # Define the substring we want to find s1 = "eeks" # Use the find() method to locate the starting index of the substring 's1' in the string 's' index = s.find(s1) #Print either satarting position of 's1' or -1 if not found ...
🌐
Medium
medium.com › @muraliairody › find-and-index-of-string-6c50c819273c
Find and Index of String
October 24, 2025 - Returns the lowest index where substring is found. Returns -1 if the substring is not found. Optional start and end parameters let you search within a slice of the string. ... text = "Hello Python" print(text.find("Python")) # 6 print(text.find("Java")) # -1 print(text.find("o")) # 4 (first occurrence)
🌐
Tutorialspoint
tutorialspoint.com › python › string_find.htm
Python String find() Method
Welcome to Tutorialspoint." str2 ... above program - ... The python string find() method returns the index where the substring is found within the range of the starting and ending index that is specified....
🌐
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 - Since the string has two substrings 'Python', the index() method returns the lowest index where the substring found in the string. The following example uses the index() method to find the substring 'Python' in the string 'Python will, Python will rock you.' within the slice str[1:]:
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-string-find
Python String find() Method - GeeksforGeeks
April 26, 2025 - find() method in Python returns the index of the first occurrence of a substring within a given string. If the substring is not found, it returns -1. This method is case-sensitive, which means "abc" is treated differently from "ABC".
🌐
Mimo
mimo.org › glossary › python › string-find
Python string.find(): Syntax, Usage, and Examples
The Python string.find() method returns the lowest index where the substring appears. If the substring isn’t found, it returns -1.
🌐
Quora
quora.com › How-do-I-find-the-index-of-a-character-in-a-string-in-Python
How to find the index of a character in a string in Python - Quora
Answer (1 of 3): You can simply use the index method. Here’s an example [code]string = 'Python' res = string.index('P') print(res) [/code]Output [code]0 [/code]
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-index-and-slice-strings-in-python-3
How To Index and Slice Strings in Python | DigitalOcean
Learn how to index and slice strings in Python 3 with step-by-step examples. Master substring extraction, negative indexing, and slice notation.