juniorStrings

What is String Concatenation and its performance impact?

Updated Apr 28, 2026

Short answer

String concatenation is the process of joining two or more strings to create a new string. Its performance depends on the programming language and string implementation, but in many languages strings are immutable, meaning each concatenation creates a new string and copies existing characters. Repeated concatenation in a loop can therefore become inefficient, often resulting in O(N²) time complexity.

Deep explanation

String concatenation combines multiple strings into a single string.

For example:

Python
first = "Hello"
second = "World"
result = first + " " + second

The result is:

TEXT
Hello World

The performance impact comes from how strings are stored and modified internally.

Immutable Strings

In many programming languages, strings are immutable. This means once a string is created, it cannot be changed.

For example:

Python
s = "Hello"
s = s + " World"

The original string "Hello" is not modified. Instead, a new string is created:

TEXT
Old string:
Hello
New string:
Hello World

The characters from the old string must be copied into the new memory location.

If a single concatenation involves:

  • String A of length M
  • String B of length N

then the operation requires copying M + N characters:

TEXT
Time complexity = O(M + N)

Repeated Concatenation Problem

A common performance issue occurs when repeatedly appending strings in a loop.

Example:

Python
result = ""
for word in words:
result = result + word

Suppose there are N words and each word has length L.

Each iteration creates a new string and copies all existing characters:

TEXT
Iteration 1: copy L characters
Iteration 2: copy 2L characters
Iteration 3: copy 3L characters
...
Iteration N: copy NL characters

The total work becomes:

TEXT
L + 2L + 3L + ... + NL
= L * (1 + 2 + 3 + ... + N)
= O(N²L)

This can become very slow for large amounts of text.

Using String Builders

Many languages provide mutable string-building utilities that avoid repeated copying.

Examples:

  • Python: list with join()
  • Java: StringBuilder
  • C#: StringBuilder
  • C++: std::string with efficient append operations

Example in Python:

Python
words = ["Hello", " ", "World"]
result = "".join(words)

The strings are collected first, then combined in one operation.

The complexity becomes:

TEXT
O(total length of all strings)

instead of repeatedly reallocating memory.

Common Concatenation Approaches

ApproachTypical Performance
Concatenating a few stringsUsually efficient enough
Repeated + inside a loopCan become O(N²)
String builder / bufferUsually O(N)
Joining a collection of stringsUsually O(N)

Language-Specific Nuance

The exact behavior depends on compiler and runtime optimizations.

For example:

  • Some languages optimize consecutive string literals:
Python
message = "Hello " "World"
  • Some runtimes optimize simple concatenation cases.
  • However, developers should still avoid repeated concatenation in large loops unless the language guarantees efficient behavior.

Real-world example

Building a large report, log file, or HTML document by repeatedly appending strings can cause unnecessary memory allocation.

Inefficient approach:

Python
report = ""
for line in lines:
report += line + "\n"

A better approach:

Python
lines_with_breaks = []
for line in lines:
lines_with_breaks.append(line + "\n")
report = "".join(lines_with_breaks)

The second approach creates the final string once instead of repeatedly creating intermediate strings.

This matters when generating large outputs such as:

  • Export files
  • API responses
  • Generated HTML pages
  • Application logs

Common mistakes

  • * Assuming string concatenation is always `O(1)` because adding one string looks like a simple operation.
  • * Ignoring string immutability when analyzing performance.
  • * Using repeated string concatenation inside large loops.
  • * Forgetting that concatenating two strings requires copying their characters.
  • * Assuming all programming languages handle strings in exactly the same way.
  • * Using a string builder for only a few small concatenations where normal concatenation is clearer.

Follow-up questions

  • Why can repeated string concatenation become O(N²)?
  • How can you optimize repeated string concatenation?
  • Are strings always immutable in every programming language?
  • What is the time complexity of concatenating two strings?
  • Why is string concatenation usually fine for small numbers of strings?

More Strings interview questions

View all →