Split on your separator at most once, and take the first piece:

sep = '...'
stripped = text.split(sep, 1)[0]

You didn't say what should happen if the separator isn't present. Both this and Alex's solution will return the entire string in that case.

Answer from Ned Batchelder on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-remove-after-substring-in-string
Python - Remove after substring in String - GeeksforGeeks
July 15, 2025 - Using find() to locate the position of the substring and then slice the string to get everything before that position.
Discussions

How can I remove everything in a string until a character(s) are seen in Python - Stack Overflow
Say I have a string and I want to remove the rest of the string before or after certain characters are seen For example, all my strings have 'egg' in them: "have an egg please" "my eggs are good" I More on stackoverflow.com
🌐 stackoverflow.com
Removing all but one instance of a phrase from a string
I’m trying to code a function that takes a sentence and a phrase as parameter and eliminates all but the first instance of the phrase in the sentence. Note, by sentence I mean a string comprised of one or more words, not a list of words. At present my code is as follows: def delete_repea... More on discuss.python.org
🌐 discuss.python.org
19
0
March 31, 2024
Python: Remove everything after first comma in string
Try .split(",")[0]. More on reddit.com
🌐 r/learnpython
4
1
May 28, 2022
Removing everything before/after a particular character
How can I remove everything before a particular character (including that character)? So, I have 'Johnson, John' and want to have only 'John'? Thank you! More on community.alteryx.com
🌐 community.alteryx.com
August 13, 2021
🌐
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)

🌐
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 - .split() #splits the string into ... character..lstrip() #strips everything before and including the character or set of characters you say. If left blank, deletes whitespace..rstrip() #strips everything out from the end up to and including the character or set of characters you give...
🌐
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 ...
🌐
Codecademy
codecademy.com › article › remove-characters-from-a-python-string
How to Remove Characters from a String in Python | Codecademy
Learn how to remove characters from a Python string using `rstrip()`, `replace()`, and `re.sub()` with examples.
Find elsewhere
🌐
Python.org
discuss.python.org › python help
Removing all but one instance of a phrase from a string - Python Help - Discussions on Python.org
March 31, 2024 - I’m trying to code a function that takes a sentence and a phrase as parameter and eliminates all but the first instance of the phrase in the sentence. Note, by sentence I mean a string comprised of one or more words, not a list of words. At present my code is as follows: def delete_repea...
🌐
Quora
quora.com › How-can-I-remove-everything-after-the-last-number-seen-in-a-String-in-Python
How to remove everything after the last number seen in a String in Python - Quora
Answer (1 of 2): * set found as a definite indication of whether a digit has been found (or not) * loop backwards through the string seeking a digit; if found break out of the loop * if, outside the loop, found was set True then truncate the input string [code]>>> s = 'abcdefg7xyz' >>> found =...
🌐
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.
🌐
Real Python
realpython.com › python-strip
How to Strip Characters From a Python String – Real Python
October 22, 2025 - Now that you understand the differences between these methods, the next section will cover the common pitfalls to avoid when using .strip(). While Python’s .strip() method may appear straightforward, using it incorrectly can lead to unintended ...
🌐
Reddit
reddit.com › r/learnpython › python: remove everything after first comma in string
r/learnpython on Reddit: Python: Remove everything after first comma in string
May 28, 2022 -

filename.txt

years list last_5

    2022; year - current, 100 users, 5 left
    2021; year - previous, 78 users, 6 left
    2020; year - prior, 60 users , 1 left
    2019; year - prior, 55 users
    2018; year - prior, 28 users

new

years list next_5

    2017; year - prior, 12 users
    2016; year - prior, 6 users
    2015; year - prior, 5 users
    2014; year - prior, 4 users
    2013; year - prior, 4 users

new

current code

with open (‘filename.txt’, ‘r+’) as f:
    lines = f.readlines()
    f.seek(0)
    f.writelines(line.strip(“\t”) for line in lines if line.strip() and line.strip(“\n”) != “new”)
    f.truncate()

current output

years list last_5
2022; year - current, 100 users, 5 left
2021; year - previous, 78 users, 6 left
2020; year - prior, 60 users , 1 left
2019; year - prior, 55 users
2018; year - prior, 28 users
years list next_5
2017; year - prior, 12 users
2016; year - prior, 6 users
2015; year - prior, 5 users
2014; year - prior, 4 users
2013; year - prior, 4 users

expected output

years list last_5
2022; year - current
2021; year - previous
2020; year - prior
2019; year - prior
2018; year - prior
years list next_5
2017; year - prior
2016; year - prior
2015; year - prior
2014; year - prior
2013; year - prior

I’ve seen that .split(“,”) splits the string using the comma and I tried adding .split(“,”, 1) to the line.strip before and after the for loop but this doesn’t appear to work.

Can anyone help me?

🌐
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.
🌐
regex101
regex101.com › library › CXChps
remove everything before a specific string
Please complete this security check to continue to regex101 · This check helps keep the site available during automated traffic spikes
🌐
GeeksforGeeks
geeksforgeeks.org › ways-to-remove-ith-character-from-string-in-python
How to Remove Letters From a String in Python - GeeksforGeeks
November 5, 2024 - Let’s start with a simple method to remove a single specific letter from a string using Python’s replace() function. replace() method is the simplest and most efficient way to remove a specific letter or substring from a string.
🌐
KNIME Community
forum.knime.com › knime analytics platform
Remove everything after a specific string - KNIME Analytics Platform - KNIME Forum Archive
April 19, 2022 - How is it possible to define a string, after which everything else in a cell should be removed?