Remove the last three characters from a string
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
Removing the last three characters from a string is simple in most languages, but the correct implementation still depends on edge cases such as short input, Unicode text, and whether failure should be silent or explicit. In Python, slicing is the usual answer, but it helps to be deliberate about the behavior you want.
The Basic Python Solution
In Python, the standard expression is text[:-3].
This returns every character except the last three. It is concise, readable, and usually all you need.
Understand What Happens With Short Strings
Python slicing is forgiving. If the string has fewer than three characters, the result is an empty string rather than an exception.
That is often convenient, but it is not always the right business behavior. Sometimes a too-short string should be treated as invalid input.
Add a Strict Variant When Length Matters
If short strings should be rejected, validate first.
The key is to choose deliberately between lenient and strict behavior rather than letting a default decide silently.
Generalize the Pattern
If this operation appears in several places, a reusable helper is clearer than repeating a magic number in many files.
This keeps the logic explicit and makes later changes easier.
Fixed Character Removal vs Real Suffix Removal
Dropping the last three characters is not always the same as removing a known suffix. If the intent is "remove .csv when present," checking the actual suffix is safer than blindly trimming by length.
This avoids accidental truncation when the input does not match the expected ending.
Think About Unicode and Display Semantics
Python string slicing works on Unicode code points, which is usually what you want. But user-facing text can contain combined emoji or grapheme clusters that do not align perfectly with visual characters.
For most plain text and identifiers, ordinary slicing is fine. For advanced user-facing text editing, especially around emoji or composed characters, you may need a grapheme-aware library instead of raw slicing.
Batch Processing Example
If you need to trim many strings, use a clear list comprehension or map-like transformation rather than scattering slicing inline throughout the pipeline.
This is small, but it still benefits from having a documented rule for short strings.
Common Pitfalls
The most common mistake is forgetting that slicing short strings does not raise an error. If too-short input is invalid, add validation.
Another issue is using a fixed-length trim when the real intent is suffix removal. In that case, check the suffix instead of blindly cutting characters.
Developers also sometimes repeat magic numbers in many places instead of creating one clear helper function.
Finally, for user-facing text with complex Unicode composition, raw slicing may not match perceived character boundaries even though the code is technically correct.
Summary
- In Python,
text[:-3]is the standard way to remove the last three characters. - Short strings return an empty string unless you add explicit validation.
- Use a helper when the same trimming rule appears in multiple places.
- Prefer actual suffix checks when the goal is to remove a known ending rather than a fixed count.
- Be cautious with complex Unicode text if visual character boundaries matter.
Related reading
- Remove trailing delimiting character from a delimited string
- Removing a list of characters in string
- Removing the first 3 characters from a string
- Replace carets with HTML superscript markup using Java
- Replace Line Breaks in a String C
- Replace non-ASCII characters with a single space
- Replacing all non-alphanumeric characters with empty strings
- Replacing instances of a character in 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.