A non-slicky method:

def index_containing_substring(the_list, substring):
    for i, s in enumerate(the_list):
        if substring in s:
              return i
    return -1
Answer from kennytm 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
re.search(): This returns None when a substring is not found. 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.
🌐
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 the substring we want to find s1 = "for" parts = s.split(s1) # Check if the substring is in the string if len(parts) > 1: # Calculate the starting index of the substring index = len(parts[0]) print(f"The substring '{s1}' is found at index {index}.") else: print(f"The 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 in a string.
Discussions

python - Get first list index containing sub-string? - Stack Overflow
For lists, the method list.index(x) ... the list of the first item whose value is x. But if I want to look inside the list items, and not just at the whole items, how do I make the most Pythoninc method for this? ... Save this answer. Show activity on this post. ... Copydef index_containing_substring(the_list, ... More on stackoverflow.com
🌐 stackoverflow.com
python - Find a substring in a string and returning the index of the substring - Stack Overflow
I have: a function: def find_str(s, char) and a string: "Happy Birthday", I essentially want to input "py" and return 3 but I keep getting 2 to return instead. Code: def find_str(s, char): ... More on stackoverflow.com
🌐 stackoverflow.com
python - Finding substring in list of strings, return index - Stack Overflow
4 Pythonic way of searching for a substring in a list · 15 How to find indexes of string in lists which starts with some substring? More on stackoverflow.com
🌐 stackoverflow.com
How do I search a list of Strings for a certain word and return the index?
How do I search the list for "Berlin" and get the index of the sentence containing "Berlin"? Loop over each element in cities with the enumerate function and check if Berlin is in each. enumerate returns a tuple with the index and the value at that index. cities = ["The capital of france is Paris", "The capital of france is Berlin"," The capital of france is London"," The capital of france is Barcelona"] query = 'Berlin' for index, value in enumerate(cities): if query in value: print(f"The sentence containing '{query}' is at index '{index}'!") >>> The sentence containing 'Berlin' is at index '1'! More on reddit.com
🌐 r/learnpython
5
1
January 25, 2021
🌐
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 - In this post, you leaned how to use Python to find the first index, the last index, and all indices of a substring in a string. You learned how to do this with regular string methods, with regular expressions, list comprehensions, as well as a custom built function.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-find-string-in-list
Python Find String in List: Methods and Examples | DigitalOcean
1 month ago - If you need to match a substring inside list elements, see the section on Searching for a substring within list elements. For more on the string formatting used here, see f-strings in Python. When you need the position of a string instead of a yes/no answer, use the list index() method.
🌐
TutorialsPoint
tutorialspoint.com › python-find-index-containing-string-in-list
Python - Find Index Containing String in List
August 16, 2023 - If an occurrence is found, its index is appended to the list ids. If no more occurrences are found, a ValueError is raised and caught, and the loop is exited. Finally, the list of indices is returned.The program uses the recursive function to set the input list and with the help of index() and append() it will find all indexes of the same string.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-extract-indices-of-substring-matches
Python - Extract Indices of substring matches - GeeksforGeeks
May 5, 2023 - In this, we perform the similar task as above method using list comprehension and enumerate() is used to get compact solution. ... # Python3 code to demonstrate working of # Extract Indices of substring matches # Using list comprehension + enumerate() # initializing list test_list = ["Gfg is good", "for Geeks", "I love Gfg", "Its useful"] # initializing K K = "Gfg" # printing original list print("The original list : " + str(test_list)) # using list comprehension and enumerate to offer compact solution res = [idx for idx, val in enumerate(test_list) if K in val] # printing result print("The indices list : " + str(res))
🌐
Better Programming
betterprogramming.pub › 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
🌐
Programiz
programiz.com › python-programming › methods › string › index
Python String index()
sentence = 'Python programming is fun.' # Substring is searched in 'gramming is fun.' print(sentence.index('ing', 10)) # Substring is searched in 'gramming is ' print(sentence.index('g is', 10, -4)) # Substring is searched in 'programming'
🌐
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), ...
🌐
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 example, the index() method returns the position of the second occurrence of the substring in the string. The following example raises a ValueError because the substring 'Java' doesn’t exist in the string 'Python will, Python will rock you.':