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
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_string_index.asp
Python String index() Method
Python Strings Slicing Strings Modify Strings Concatenate Strings Format Strings Escape Characters String Methods String Exercises Code Challenge Python Booleans
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ string โ€บ index
Python String index()
If substring exists inside the string, it returns the lowest index in the string where substring is found.
๐ŸŒ
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.
๐ŸŒ
Built In
builtin.com โ€บ software-engineering-perspectives โ€บ python-substring-indexof
5 Ways to Find the Index of a Substring in Python | Built In
The index() method in Python finds the first occurrence of a specific character or element in a string or list. If the character or element is found, its index value will be returned.
๐ŸŒ
Codecademy
codecademy.com โ€บ learn โ€บ dacp-python-fundamentals โ€บ modules โ€บ dscp-python-strings โ€บ cheatsheet
Python Fundamentals: Python Strings Cheatsheet | Codecademy
It can be used to compute the length of strings, lists, sets, and other countable objects. ... The Python string method .find() returns the index of the first occurrence of the string passed as the argument.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ find-the-index-of-a-substring-in-python
Find the Index of a Substring in Python - GeeksforGeeks
July 23, 2025 - In this article, we will explore some simple and commonly used methods to find the index of a substring in Python. The find() method searches for the first occurrence of the specified substring and returns its starting index.
๐ŸŒ
Real Python
realpython.com โ€บ lessons โ€บ index-python-substring
Use .index() to Get the Location of the Substring (Video) โ€“ Real Python
00:25 It is aptly named .index(). So you can say text_lower.index() and then pass it as an argument the substring, and then Python will return to you the index position of the first letter of the first occurrence of the substring in your string.
Published ย  April 4, 2023
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ json.html
json โ€” JSON encoder and decoder
Keys in key/value pairs of JSON are always of the type str. When a dictionary is converted into JSON, all the keys of the dictionary are coerced to strings. As a result of this, if a dictionary is converted into JSON and then back into a dictionary, the dictionary may not equal the original one.
๐ŸŒ
Java Guides
javaguides.net โ€บ 2024 โ€บ 12 โ€บ python-string-index-method.html
Python String index() Method
December 13, 2024 - The index() method in Python is used to find the first occurrence of a specified substring within a string. If the substring is found, the method returns the index of its first occurrence. If the substring is not found, it raises a ValueError.
๐ŸŒ
Pandas
pandas.pydata.org โ€บ docs โ€บ reference โ€บ api โ€บ pandas.DataFrame.loc.html
pandas.DataFrame.loc โ€” pandas 3.0.4 documentation
Access a group of rows and columns by label(s) or a boolean array ยท A single label, e.g. 5 or 'a', (note that 5 is interpreted as a label of the index, and never as an integer position along the index)
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-find-index-containing-string-in-list
Python - Find Index containing String in List - GeeksforGeeks
July 23, 2025 - It involves identifying the position where a specific string appears within the list. index() method in Python is used to find the position of a specific string in a list.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-index-and-slice-strings-in-python
How to Index and Slice Strings in Python? - GeeksforGeeks
July 15, 2025 - String Indexing allows us to access individual characters in a string. Python treats strings like lists where each character is assigned an index, starting from 0.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-find-position-of-a-character-in-given-string
Find position of a character in given string - Python - GeeksforGeeks
July 11, 2025 - Given a string and a character, our task is to find the first position of the occurrence of the character in the string using Python. For example, consider a string s = "Geeks" and character k = 'e', in the string s, the first occurrence of ...
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ python string index() function | get index of substring
Python string index() Function | Get index of substring - EyeHunts
May 18, 2021 - Python string index() Function is determines if string substring occurs in string (sentence) or in a substring of string. This function is same as find(), ..
๐ŸŒ
DataCamp
datacamp.com โ€บ tutorial โ€บ python-string-contains
Python String Search and Replace: in, .find(), .index(), . ...
March 31, 2026 - Practical patterns for finding, counting, and replacing substrings in modern Python. ... Itโ€™s common to reach for .find() or .index() to check if text exists in a stringโ€”and then run into odd behavior like a ValueError or a missed match at the start of the string.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ python string index() method
Python String index() Method - Scaler Topics
January 2, 2024 - On your target string, you call the string.index(substring)' method, where string is the string you want to search in and substring is the string you want to locate. Python begins searching the target string for the first occurrence of the substring.
๐ŸŒ
CodeHS
codehs.com โ€บ tutorial โ€บ 13725
Tutorial: Indexing Python Strings | CodeHS
Learn how to access specific items by indexing through Python strings.