>>> 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 Python substring is a sequence of characters that are part of an original string. In Python, substrings can be obtained by using the slicing feature on a string variable.
🌐
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.
🌐
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 operator is intuitive and readable, making it a straightforward way to evaluate substring existence. By the end of this tutorial, you’ll understand that: The in membership operator is the recommended way to check if a Python string contains a substring.
🌐
Sentry
sentry.io › sentry answers › python › extract a substring from a string in python
Python Substring Extraction With Slice Notation | Sentry
3 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
Find elsewhere
🌐
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 - You can also define substring as a sequence of characters within a string. From the previous example, python substrings can be “Friday”, “at”, “meet”, and others.
🌐
Built In
builtin.com › software-engineering-perspectives › python-substring-indexof
5 Ways to Find the Index of a Substring in Python | Built In
A string in Python can be sliced using the slice() method. You can specify which index value in the string to begin slicing, which index to end slicing and how many steps to slice at (if wanting to skip every other character, etc.). The result will return the characters in a string based on the slice specifications. Substrings can be fetched from a larger string in Python using the slice() method.
🌐
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....
🌐
YouTube
youtube.com › watch
Identifying a Substring Within a Python String - YouTube
If you’re new to programming or come from a programming language other than Python, you may be looking for the best way to check whether a string contains an...
Published   April 6, 2023
🌐
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?

🌐
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.
🌐
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.
🌐
Better Stack
betterstack.com › community › questions › how-to-get-substring-in-python
How do I get a substring of a string in Python? | Better Stack Community
January 26, 2023 - To get a substring of a string in Python, you can use the string slicing notation, which is string[start:end], where start is the index of the first character of the substring, and end is the index of the character just after the last character ...
🌐
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.
🌐
YouTube
youtube.com › watch
Substrings and Index of Strings in Python - Python Tutorial - YouTube
If you liked the content, please consider checking out my Patreon! - https://www.patreon.com/CodingUnderPressure/membership Hey everyone! Today we are going ...
Published   December 4, 2019
🌐
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, one could check for exact match of a string, x = 'ab' match x: case 'ab' | 'c': print(1) gives 1 but this pipe operator does not work for strings outside of match case statement. expected scenario - pipe operator works for strings ...