>>> x = "Hello World!"
>>> x[2:]
'llo World!'
>>> x[:2]
'He'
>>> x[:-2]
'Hello Worl'
>>> x[-2:]
'd!'
>>> x[2:-2]
'llo Worl'

Python calls this concept "slicing" and it works on more than just strings. Take a look here for a comprehensive introduction.

Answer from Paolo Bergantino on Stack Overflow
🌐
W3Schools
w3schools.com › python › python_strings.asp
Python Strings
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Discussions

What is the most efficient way to find substrings in strings?
Easiest solution is to just use the 'in' operator like this: if "Leonardo DiCaprio" in article_title: return True And according to the top answer here it's also the fastest: whats-a-faster-operation-re-match-search-or-str-find Edit:cant get that return statement to indent for the life of me, you get the idea More on reddit.com
🌐 r/learnpython
22
4
January 11, 2022
How do I remove a substring from a string by indicating what I am wanting to remove with a string variable?
replace is a method of str so by calling it by itself it's treating the first argument as the str instance. It's equivalent to substring.replace(''), which, as you can see from the error message you're getting, is missing an argument. You need: your_string.replace(substring, '') More on reddit.com
🌐 r/learnpython
10
30
July 29, 2022
🌐
Codecademy
codecademy.com › docs › python › substrings
Python | Substrings | Codecademy
June 18, 2025 - A substring in Python is a contiguous sequence of characters extracted from an original string.
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-substring-a-string-in-python
How to Substring a String in Python - GeeksforGeeks
July 23, 2025 - In this example, we are trying to extract the starting word from the string.we used string slicing to extract the substring "Geeks" from the beginning of the string.
🌐
Sentry
sentry.io › sentry answers › python › extract a substring from a string in python
Python Substring Extraction With Slice Notation | Sentry
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
🌐
Medium
medium.com › nerd-for-tech › python-substrings-everything-you-need-to-know-4cca526c07eb
Python Substrings: Everything You Need to Know | Nerd For Tech
July 21, 2021 - In python, a String is a sequence of characters that may contain special characters or alphanumeric characters, for example, “we meet on Friday at 08:00 am”. It is possible to access sub-parts of the string commonly known as a python substring.
Find elsewhere
🌐
Real Python
realpython.com › python-string-contains-substring
How to Check if a Python String Contains a Substring – Real Python
December 1, 2024 - In this code snippet, you use the membership operator to check whether "secret" is a substring of raw_file_content. If it is, then you’ll print a message to the terminal. Any indented code will only execute if the Python string that you’re checking contains the substring that you provide.
🌐
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.
🌐
Udemy
blog.udemy.com › home › it & development › software development › substrings in python and the many ways you can use them
Substrings in Python and Ways You Can Use Them - Udemy Blog
April 14, 2026 - This article will introduce you ... If a string is a sequence of characters, a Python substring is simply a sequence of characters within another string....
🌐
GeeksforGeeks
geeksforgeeks.org › python › find-the-index-of-a-substring-in-python
Find the Index of a Substring in Python - GeeksforGeeks
July 23, 2025 - 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.
🌐
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?

🌐
Python
docs.python.org › 3 › library › string.html
string — Common string operations
Template strings provide simpler string substitutions as described in PEP 292. A primary use case for template strings is for internationalization (i18n) since in that context, the simpler syntax and functionality makes it easier to translate than other built-in string formatting facilities in Python.
🌐
IONOS
ionos.com › digital guide › websites › web development › python substring
How to create and edit Python substrings
July 19, 2023 - In Python, there are different ways to create sub­strings or to check for sub­strings within a string. A substring is es­sen­tial­ly just a portion of a Python string, with a string being a sequence of char­ac­ters of any length.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-substring-a-string-in-python
How to Substring a String in Python
January 3, 2020 - Python offers many ways to substring a string. 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. If start is not in...
🌐
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
Here's our challenge: we have two lists of strings of the same length, one containing the "original" strings and the other, the "substrings". We're to identify all occurrences of each substring within its corresponding original string and return a list of the starting indices of these occurrences.
🌐
Reddit
reddit.com › r/learnpython › how do i remove a substring from a string by indicating what i am wanting to remove with a string variable?
r/learnpython on Reddit: How do I remove a substring from a string by indicating what I am wanting to remove with a string variable?
July 29, 2022 -

I have been trying to get this one working all day. I have a string that contains a substring that I would like to remove but the typical replace function isn't working in the way I would like to use it. I have the substring I want to remove stored in a string variable while having the string I am modifying in another variable. I tried to use the function like this

inspected = replace(substring, '')

but for some reason I get an error indicating that only one argument was passed rather than two. I think this function only accepts quotes rather than the variables themselves unless I am doing something wrong. I have been searching for this one all day but to no avail.

example

inspected = "iamthewalrus"
substring = "walrus"
inspected = replace(substring,"")

🌐
Python.org
discuss.python.org › python help
Use of pipe operator to search for substring in string - Python Help - Discussions on Python.org
June 2, 2022 - current scenario - 1) ('c' or 'a') in 'ab' gives, False as ('c' or 'a') evaluates to 'c' the substring check could be obtained, but need to write it using any builtin any([(i in 'ab') for i in ('c', 'a')]) gives, True in a match case statement, ...
🌐
Scrimba
scrimba.com › s04334h
Substring a String in Python
January 8, 2023 - If someone has shared this link with you, they may need to invite you or change the privacy settings of the scrim
🌐
W3Schools
w3schools.com › python › python_ref_string.asp
Python String Methods
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
Python documentation
docs.python.org › 3 › library › stdtypes.html
Built-in Types — Python 3.14.6 documentation
The find() method should be used only if you need to know the position of sub. To check if sub is a substring or not, use the in operator: