How to insert a character in a string at a certain position?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Inserting a character into a string at a specific position is a basic text-manipulation task, but the implementation depends on whether the language treats strings as immutable. In most languages, the answer is either "build a new string from slices" or "use a mutable helper such as StringBuilder."
The Core Idea
If a string is immutable, you cannot modify it in place. Instead, you create:
- the part before the insertion point
- the new character
- the part after the insertion point
In Python, that looks like this:
The insertion index can be anywhere from 0 through len(text). Inserting at the end is just appending.
Python Uses Slicing
Because Python strings are immutable, slicing is the idiomatic approach:
That prints abXcd.
If you need several small edits, convert to a list first:
That pattern can be clearer when many insertions happen in one workflow.
Java Often Uses StringBuilder
In Java, a String is also immutable. For one insertion, substring concatenation works, but StringBuilder is usually cleaner:
StringBuilder.insert is especially helpful if you expect more edits after the first one.
JavaScript Works Like Python
JavaScript strings are immutable too, so the standard solution is slicing:
The technique is the same even though the API names differ.
Validate Indexes Carefully
The most common boundary rule is:
- '
0means insert before the first character' - '
lenmeans insert after the last character' - anything outside that range is invalid
That matters because insertion is slightly different from replacement. For insertion, index == length is valid.
This distinction is easy to miss when you reuse code written for replacement or assignment.
Think About Repeated Operations
For one insertion, almost any correct solution is fine. For many insertions in a loop, repeated string rebuilding can become inefficient.
In those cases:
- use a mutable helper structure
- batch edits when possible
- avoid repeated full-string copies
That advice matters most for large texts or performance-sensitive code, not for small application strings.
Common Pitfalls
One common mistake is trying to assign directly into an immutable string, such as changing one index in place.
Another issue is forgetting that insertion allows the index to equal the string length. Rejecting that case blocks valid append-style inserts.
A third problem is not validating the replacement input and accidentally inserting a multi-character string when the code was meant to insert only one character.
Finally, when performing many edits, developers sometimes keep concatenating inside large loops and then wonder why the code slows down.
Summary
- Most languages treat strings as immutable, so insertion usually means building a new string.
- In Python and JavaScript, slicing is the standard solution.
- In Java,
StringBuilder.insertis usually the cleanest option. - Validate indexes carefully, especially the allowed end-of-string case.
- For many edits, prefer mutable helper structures over repeated string concatenation.

