Remove characters after specific character in string, then remove substring?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
This kind of string-cleaning problem usually has two steps: cut the string at a delimiter, then remove a specific substring from what remains. The important detail is that the order matters. If you trim after the delimiter first, the second cleanup only affects the part you kept. If you remove the substring first, you may change where the delimiter appears or remove text you actually intended to keep.
Step 1: Remove Everything After a Delimiter
A clear Python solution uses split or partition.
This keeps only the part before the first #.
Using split(..., 1) is useful because it limits the operation to the first delimiter.
Step 2: Remove the Target Substring
Once you have the kept portion, remove the unwanted substring.
This prints abc.
That two-step sequence is often easier to read than trying to compress everything into one complicated regular expression.
One Function That Does Both
This keeps the logic explicit and easy to test.
Why Order Matters
Suppose the substring appears after the delimiter as well as before it. If you remove the substring first, you may do unnecessary work or change content that would have been discarded anyway.
The normal strategy is:
- trim at the delimiter
- clean the remaining kept part
That matches the human description of the task more closely.
When partition Is Cleaner
partition is a nice alternative because it always returns exactly three parts.
Some developers prefer this because it makes the delimiter handling more explicit than split.
When Regular Expressions Help
If the delimiter or removable pattern is more complex than a literal string, re.sub can help. But for simple literal cleanup, regular expressions are often unnecessary complexity.
A lot of string-processing code becomes easier to maintain once you separate literal operations from true pattern matching.
Common Pitfalls
The biggest mistake is performing the steps in the wrong order and accidentally cleaning text that would have been discarded anyway.
Another mistake is using unrestricted replace without checking whether you meant to remove all occurrences or only specific ones.
A third issue is reaching for regex immediately when plain string operations such as split, partition, and replace already solve the problem clearly.
Summary
- First cut the string at the delimiter, then remove the target substring from the kept part
- '
split(delimiter, 1)andpartition(delimiter)are both good ways to stop at the first delimiter' - Use
replacefor literal substring removal when regex is unnecessary - Keep the two operations explicit so the code stays easy to test and maintain
- The main design choice is not syntax but the order in which the cleanup steps should happen
Related reading
- Remove final character from string
- Remove Last Two Characters in a String
- Remove Last Two Characters in a String
- Remove text in-between delimiters in a string using a regex?
- Remove the last three characters from a string
- Remove trailing delimiting character from a delimited string
- Removing a list of characters in string
- Removing the first 3 characters from a string
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.