Remove Last Two Characters in 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 two characters from a string is a small operation, but the exact code depends on the language and on what you mean by "character." In most cases, the solution is a slice or substring operation that returns everything except the final two positions.
Use Slicing or Substring Operations
In languages with slicing support, this is usually one expression. Python is the classic example:
The result is backe. Python strings are immutable, so the original string is unchanged and trimmed is a new string.
In JavaScript, the equivalent idea uses slice:
This says "start at index zero and stop two positions before the end." It is concise and easy to read once you know that negative indexes count backward from the end in slice.
Handle Short Strings Safely
The next question is what should happen when the input has fewer than two characters. Some languages return an empty string naturally for these cases, while others need a manual guard or a more careful substring call.
A safe Python helper looks like this:
And a safe JavaScript helper follows the same logic:
Adding the guard makes the intent explicit, which is helpful when the input size is not guaranteed.
Remember That Strings Are Usually Immutable
Many beginners expect string manipulation to change the original value in place. In most modern languages, strings are immutable, so a slice or substring creates a new string instead.
That matters when you are cleaning data in a loop or building a parser. If you forget to store the result, nothing changes:
This still prints report!! because the sliced string was never assigned back anywhere.
"Two Characters" Can Be Harder Than It Looks
For basic ASCII text, removing the last two characters is straightforward. Unicode can make it trickier. Some user-visible characters are composed of multiple code points, especially emojis and accented characters. A simple slice removes code units or code points according to the language, not necessarily full grapheme clusters.
If you are trimming ordinary identifiers, filenames, or protocol strings, simple slicing is fine. If you are working with user-facing international text, you may need a Unicode-aware library rather than a naive two-position slice.
That distinction matters more in frontend text handling and messaging systems than in ordinary backend string cleanup, but it is worth knowing so the simple solution is used in the right context.
Common Pitfalls
The most common mistake is assuming the original string changes in place. In most languages, it does not.
Another issue is forgetting to handle inputs shorter than two characters, especially in languages where substring boundaries can raise errors.
People also sometimes copy a slicing expression from one language into another. Negative indexes and slice semantics are not identical everywhere, so use the idiom for the language you are actually writing.
Summary
- Removing the last two characters is usually a slice or substring operation.
- Python uses
text[:-2], while JavaScript commonly usestext.slice(0, -2). - Short strings may need an explicit guard depending on the language and desired behavior.
- Strings are usually immutable, so store the returned value.
- For Unicode-heavy user text, "two characters" may require more than a simple raw slice.
Related reading
- 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
- Replace carets with HTML superscript markup using Java
- Replace Line Breaks in a String C
- Replace non-ASCII characters with a single space
.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.