๐ String Concatenation
String Concatenation means joining two or more strings end-to-end to form a new string.
Example:
"Hello" + "World" = "HelloWorld"
In Data Structures, string concatenation is important because:
- Strings are sequences (like arrays).
- Concatenation involves copying characters.
- Performance depends on the immutability of strings.
๐ Strings as Data Structure
A string is essentially:
๐ A sequence of characters stored in contiguous memory, similar to an array.
In many languages (including Python), strings are:
- Immutable
- Fixed-size once created
- Any modification creates a new string object
This affects performance of concatenation.
๐ String Concatenation โ Time Complexity
Since strings are immutable, concatenating strings like:
s = s + "a"
creates a new string each time.
Time Complexity:
[
O(n)
]
Where n = length of resulting string.
If you repeat this in a loop, total cost becomes:
[
O(n^2)
]
This is important in competitive programming and algorithm design.
๐ String Concatenation in Python
Python provides multiple ways:
1. Using + Operator
s = "Hello"
s = s + "World"
print(s) # HelloWorld
โ Easy to read
โ Costly for repeated concatenation
Time complexity each time = O(n)
2. Using += Operator
s = ""
for i in range(5):
s += "a"
print(s) # aaaaa
Internally same as s = s + "a"
๐ Still O(n) per concatenation โ O(nยฒ) for loops.
3. Using join() (Best Way)
Recommended for joining multiple strings efficiently.
words = ["data", "structure", "python"]
result = "".join(words)
print(result) # datastructurepython
โ Very fast
โ O(n) total
โ Used in production code
โ No repeated copying
Internally uses an optimized single-pass allocation.
4. Using StringIO (for heavy concatenation)
When continuously building large strings:
from io import StringIO
buffer = StringIO()
buffer.write("Hello")
buffer.write("World")
result = buffer.getvalue()
print(result) # HelloWorld
โ Efficient for huge outputs
โ Avoids repeated copying
โ Useful in competitive coding
5. Using f-Strings / format()
Not ideal for loops, but good for small operations:
name = "Alice"
age = 20
info = f"{name} is {age} years old."
๐ Performance Comparison Table
| Method | Recommended | Time Complexity |
|---|---|---|
+ | โ for loops | O(nยฒ) total |
+= | โ for loops | O(nยฒ) total |
"".join(list) | โโ Best | O(n) |
StringIO | โ for very large concatenation | O(n) |
format(), f-strings | โ for readability | O(n) |
๐ Example to Show Performance Difference
โ Slow method:
s = ""
for i in range(10000):
s += "a" # O(nยฒ)
โ Fast method:
parts = []
for i in range(10000):
parts.append("a")
s = "".join(parts) # O(n)
๐ String Concatenation as a Data Structure Problem
In DSA, strings are treated like arrays.
Concatenation involves:
- Allocating memory for new string
- Copying characters from both strings
- Constructing final string
- Returning new reference
This is why repeated concatenation becomes costly โ repeated copying.
To optimize, Python uses:
- rope data structures in some languages
- StringBuilder in Java
- StringIO in Python
- join() which builds final string efficiently
๐ Summary (Exam Notes)
- String concatenation = joining two strings.
- Python strings are immutable โ every concat creates new object.
+and+=are O(n) per operation โ O(nยฒ) in loops."".join()is the most efficient โ O(n).- For large concatenation, use StringIO.
- Important concept in DSA because concatenation affects time complexity.
