You can use str.find method with a simple indexing :

>>> s="have an egg please"
>>> s[s.find('egg'):]
'egg please'

Note that str.find will returns -1 if it doesn't find the sub string and will returns the last character of your string.So if you are not sure that always your string is contain the sub string you better to check the value of str.find before using it.

>>> def slicer(my_str,sub):
...   index=my_str.find(sub)
...   if index !=-1 :
...         return my_str[index:] 
...   else :
...         raise Exception('Sub string not found!')
... 
>>> 
>>> slicer(s,'egg')
'egg please'
>>> slicer(s,'apple')
Sub string not found!
Answer from Kasravnd on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-after-substring-in-string
Python - Remove after substring in String - GeeksforGeeks
July 15, 2025 - Indexing with [0], the code retrieves the part of the string before the substring, resulting in "Hello, this is a " Using find() to locate the position of the substring and then slice the string to get everything before that position.
Discussions

string - How to remove all characters before a specific character in Python? - Stack Overflow
I'd like to remove all characters before a designated character or set of characters (for example): intro = " I'm Tom." Now I'd like to remove the before I'm (or more specifically,... More on stackoverflow.com
🌐 stackoverflow.com
replace - How to remove all characters after a specific character in python? - Stack Overflow
I have a string. How do I remove all text after a certain character? (In this case ...) The text after will ... change so I that's why I want to remove all characters after a certain one. More on stackoverflow.com
🌐 stackoverflow.com
Removing a all specific text after a word in Python.
A site I wish someone had shown me when I was learning... Drop your code in here and step through it ("Visualize Execution") and watch what it's doing. See if you can figure it out. If not feel free to come back :) https://pythontutor.com/visualize.html More on reddit.com
🌐 r/learnpython
23
9
July 3, 2022
How to remove part of string after certain character in python?

There are a few ways. Here are a few off the top of my head:

>>> s = "abcd//efgh"

>>> s.find("/")
4
>>> s[:s.find("/")]
'abcd'

>>> s.split("/")
['abcd', '', 'efgh']
>>> s.split("/", maxsplit=1)
['abcd', '/efgh']
>>> s.split("/", maxsplit=1)[0]
'abcd'

>>> import re
>>> re.sub("/.*$", "", s)
'abcd'

The last is overkill here and I wouldn't use it, but regexs are often appropriate for doing search & replace operations. Either of the first two would work pretty well. The first depends on the search string appearing though. Otherwise, s.find will return -1 and then s[:-1] will lop off the last character:

>>> s = "abcdef"
>>> s[:s.find("/")]
'abcde'
More on reddit.com
🌐 r/AskProgramming
4
5
July 2, 2017
🌐
Medium
medium.com › @Alexander_H › removing-characters-before-after-and-in-the-middle-of-strings-fb4930cce76a
Removing characters before, after, and in the middle of strings | by This Time Is Different | Medium
October 30, 2017 - When working with real-world datasets ... into two tuples around whatever character it was given and deletes that character..lstrip() #strips everything before and including the character or set of characters you say...
🌐
Bobby Hadz
bobbyhadz.com › blog › python-remove-everything-after-character
Remove everything Before or After a Character in Python | bobbyhadz
April 9, 2024 - To remove everything before a character in a string: Use the str.find() method to get the index of the character. Use string slicing and set the start index to the index of the character.
🌐
pythontutorials
pythontutorials.net › blog › how-can-i-remove-everything-in-a-string-until-a-character-s-are-seen-in-python
How to Remove Everything Before or After a Specific Substring in Python: Examples & Methods — pythontutorials.net
text = "apple, banana, cherry" substring = "banana" before, sub, after = text.partition(substring) result = before + sub # Combine "before" and the substring print(result) # Output: "apple, banana" (everything before/including "banana")
🌐
Note.nkmk.me
note.nkmk.me › home › python
Remove a Substring from a String in Python | note.nkmk.me
April 23, 2025 - This article explains how to remove a substring (i.e., a part of a string) from a string in Python. Remove a substring by replacing it with an empty stringRemove exact match string: replace()Remove su ...
Find elsewhere
🌐
Sololearn
sololearn.com › en › Discuss › 2986076 › how-would-i-remove-everything-before-a-certain-word-once-the-word-is-found-python
How would I remove everything before a certain word once ...
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
🌐
Esri Community
community.esri.com › t5 › python-questions › remove-all-characters-before-a-certain-character › td-p › 197897
Remove all characters before a certain character with Python using Field Calculator
June 2, 2022 - Solved: I have a series of strings that I need to eliminate a series of text. The text can be variable in length, so doing any trim or strip functions will not
🌐
Reddit
reddit.com › r/learnpython › removing a all specific text after a word in python.
r/learnpython on Reddit: Removing a all specific text after a word in Python.
July 3, 2022 -

Hello,

I want to remove the entire substring after a particular word in Python.

For instance, if the word is "Excerpt," all text after the word would be removed.

However, my code is not accomplishing that task.

If the word exists in the string, I would find the index of it.

After that, I would subtract the len(text)-index of the word, which should point to the word.

Am I not returning from 0-index of the word, hence removing the word, and everything after the word.

What is the issue with my code?

str1 = 'Excerpt'
if str1 in text.split():
strLength = text.find(str1)
a = text[0:len(text)-strLength]
print(a)

🌐
Edureka Community
edureka.co › home › community › categories › python › how to remove all characters before a specific...
How to remove all characters before a specific character in Python | Edureka Community
February 27, 2019 - I want to remove all characters before a designated character or set of characters. For example : intro = " ... I want to remove the before I'm ?
🌐
Bomberbot
bomberbot.com › python › python-remove-everything-after-a-substring-in-a-string
Python: Remove Everything After a Substring in a String - Bomberbot
July 2, 2025 - Given a string and a substring, we aim to create a new string that contains everything from the original string up to, but not including, the specified substring. If the substring is not found, we typically want to return the original string unchanged. ... original_string = "Hello, this is a sample string." substring = "sample" desired_result = "Hello, this is a " One of the simplest approaches leverages Python's split() method: def remove_after_substring(s, substring): return s.split(substring)[0] s = "Hello, this is a sample string." result = remove_after_substring(s, "sample") print(result) # Output: "Hello, this is a "
🌐
regex101
regex101.com › library › CXChps
regex101: remove everything before a specific string
To get a variable name from a source code: The variable name is before the '=' (equal sign) This is the way to detect. Problem: Only 1 variable can get from 1 line. Unfortunately, this can get variable between after "//" and before ";" too. I made this for fake script debugger.
🌐
Tutorial Reference
tutorialreference.com › python › examples › faq › python-how-to-remove-everything-before-or-after-a-character-in-a-string
How to Remove Parts of Strings Before or After a Character in Python | Tutorial Reference
[0]: Selects the first element of the resulting list (everything before the last separator). rpartition() splits the string at the last occurrence of the separator and returns a tuple: (part_before, separator, part_after). ... Use find() to locate the index of the first occurrence. Use slicing to extract the substring from the found index until the end.
🌐
DigitalOcean
digitalocean.com › community › tutorials › python-remove-character-from-string
How to Remove Characters from a String in Python | DigitalOcean
Remove characters from a Python string with replace(), translate(), re.sub(), and slicing. Compare methods, see examples, and pick the right approach.