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 › dsa › check-string-substring-another
Check if a string is substring of another - GeeksforGeeks
// C program to check if a string is substring of other // using nested loops #include <stdio.h> #include <string.h> // Function to find if pat is a substring of txt int findSubstring(char *txt, char *pat) { int n = strlen(txt); int m = strlen(pat); ...
Published   April 29, 2018
🌐
Reddit
reddit.com › r/learnpython › what is the most efficient way to find substrings in strings?
r/learnpython on Reddit: What is the most efficient way to find substrings in strings?
January 11, 2022 -

Hello,

There are several ways to find substrings in string, You could use substring in string you could use string.index(substring), or you could use string.find(substring) or even use regex.

I'm trying to understand if there is a significant difference between them for my use case, which is finding people names in article titles for example:

I want to check if Leonardo DiCaprio is in Leonardo DiCaprio gets called out for boarding superyacht: ‘eco-hypocrite’

What I usually do is:

def is_substring_in_string(substring: str, string: str):        
    #same thing as before but on the article title.
    string_alphanumeric = ''.join(e for e in string if e.isalnum()).lower()

    return substring in string_alphanumeric

def main():
    names = ["Harrison Ford","Leonardo DiCaprio","Eddie Murphy","Bruce Willis","Will Smith"]

    #removes unwanted charecters such as !@#$%^&*() etc and converts to lower case
    names_alnum_lower = [''.join(e for e in x if e.isalnum()).lower() for x in names]

    article_title = "Leonardo DiCaprio gets called out for boarding superyacht: eco-hypocrite"

    for idx, lower_name in enumerate(names_alnum_lower):
        if is_substring_in_string(lower_name,article_title):
            print(f"Actor Name: '{names[idx]}' is in article title '{article_title}'")

if __name__ == '__main__':
    main()

Imagine there are a bunch of articles and people's names. Is this method acceptable or will I be better off using regex or something else?

🌐
freeCodeCamp
freecodecamp.org › news › python-find-how-to-search-for-a-substring-in-a-string
Python find() – How to Search for a Substring in a String
July 25, 2022 - Since the search starts at position 3, the return value will be the first instance of the string containing 'w' from that position and onwards. You can also narrow down the search even more and be more specific with your search with the end parameter: fave_phrase = "Hello world!" # find the index of the letter 'w' between the positions 3 and 8 search_fave_phrase = fave_phrase.find("w",3,8) print(search_fave_phrase) #output # 6 · As mentioned earlier, if the substring you specify with find() is not present in the string, then the output will be -1 and not an exception.
🌐
Programiz
programiz.com › python-programming › methods › string › find
Python String find()
print(quote.find('small things', 10)) # Substring is searched in ' small things with great love' print(quote.find('small things', 2)) # Substring is searched in 'hings with great lov'
🌐
Real Python
realpython.com › python-string-contains-substring
How to Check if a Python String Contains a Substring – Real Python
December 1, 2024 - The in membership operator is the recommended way to check if a Python string contains a substring. Converting input text to lowercase generalizes substring checks by removing case sensitivity. The .count() method counts occurrences of a substring, while .index() finds the first occurrence’s ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.string.substring
String.Substring Method (System) | Microsoft Learn
You call the Substring(Int32) method to extract a substring from a string that begins at a specified character position and ends at the end of the string. The starting character position is zero-based; in other words, the first character in ...
Find elsewhere
🌐
Python Tutorial
pythontutorial.net › home › python string methods › python string find()
Python String find(): How to Find a Substring in a String Effectively
November 16, 2023 - The find() is a string method that finds a substring in a string and returns the index of the substring.
🌐
Codecademy
codecademy.com › article › how-to-check-if-a-string-contains-a-substring-in-python
How to Check if a String Contains a Substring in Python | Codecademy
The .find() method in Python is another built-in method that we can use to locate substrings within a string. This method checks the main string and returns the index of the first occurrence of the given substring, just like .index(). If the ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript | MDN
This feature is well established ... since July 2015. ... The substring() method of String values returns the part of this string from the start index up to and excluding the end index, or to the end of the string if no end index is supplied....
🌐
Unstop
unstop.com › home › blog › c++ string find() | examples to find substrings & more!
C++ String Find() | Examples To Find Substrings & More!
July 1, 2025 - In simple terms, the find() function ... in C++ language, the C++ library provides a .find() function which returns the index of the first occurrence of a substring in a string....
🌐
UiPath Community
forum.uipath.com › help
Find Substring of a String in Another String - Help - UiPath Community Forum
May 9, 2020 - Hi, I have two strings, 0017-Part and INV-0017 | P/O 9711, I need to check if the second string contains any substring of the first string.
🌐
LearnDataSci
learndatasci.com › solutions › python-string-contains
Python String Contains – See if String Contains a Substring – LearnDataSci
The easiest and most effective way to see if a string contains a substring is by using if ... in statements, which return True if the substring is detected. Alternatively, by using the find() function, it's possible to get the index that a substring starts at, or -1 if Python can't find the ...
🌐
Programiz
programiz.com › java-programming › examples › check-string-contains-substring
Java Program to Check if a string contains a substring
class Main { public static void main(String[] args) { // create a string String txt = "This is Programiz"; String str1 = "Programiz"; String str2 = "Programming"; // check if name is present in txt // using contains() boolean result = txt.contains(str1); if(result) { System.out.println(str1 + " is present in the string."); } else { System.out.println(str1 + " is not present in the string."); } result = txt.contains(str2); if(result) { System.out.println(str2 + " is present in the string."); } else { System.out.println(str2 + " is not present in the string."); } } }
🌐
LeetCode
leetcode.com › problems › find-the-index-of-the-first-occurrence-in-a-string
Find the Index of the First Occurrence in a String - LeetCode
Can you solve this real interview question? Find the Index of the First Occurrence in a String - Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Example 1: Input: haystack = "sadbutsad", needle = ...
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › string-find-in-cpp
String find() in C++ - GeeksforGeeks
In C++, string find() is a built-in library function used to find the first occurrence of a substring in the given string.
Published   July 11, 2025