string manipulation
programming
text processing
coding tutorial
remove characters

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.

Practice ML system design

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:

python
text = "backend"
trimmed = text[:-2]
print(trimmed)

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:

javascript
const text = "backend";
const trimmed = text.slice(0, -2);
console.log(trimmed);

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:

python
1def remove_last_two(text: str) -> str:
2    if len(text) <= 2:
3        return ""
4    return text[:-2]
5
6
7print(remove_last_two("hi"))
8print(remove_last_two("hello"))

And a safe JavaScript helper follows the same logic:

javascript
1function removeLastTwo(text) {
2  if (text.length <= 2) {
3    return "";
4  }
5  return text.slice(0, -2);
6}
7
8console.log(removeLastTwo("hi"));
9console.log(removeLastTwo("hello"));

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:

python
text = "report!!"
text[:-2]
print(text)

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 uses text.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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.