PCAP Strings Practice Question
Which of the following is the BEST practice for building a large string by concatenating many smaller strings in Python?
⚠ Common exam trap
Python Institute often tests the misconception that `+=` is acceptable for all string building, or that `sum` or non-existent methods like `str.concat` are valid, when in fact `''.join()` is the only efficient and correct approach for large concatenations.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
result = ''.join(parts)
`''.join(parts)` is the most efficient way to concatenate a large number of strings in Python. It allocates memory once for the final string by iterating over the list and copying each part into the result buffer, avoiding the O(n²) time complexity of repeated concatenation in a loop.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
result = ''.join(parts)
Why this is correct
''.join(parts) is the canonical and most efficient way to combine a sequence of strings in Python. It allocates exactly one new string object, iterates over parts once, and copies each part's characters into a pre-sized buffer, achieving O(n) time and minimal memory overhead. This method clearly communicates intent and avoids the quadratic behavior of repeated concatenation.
- ✗
result = sum(parts, '')
Why it's wrong here
sum(parts, '') works syntactically only because sum starts with the initial value '' and performs repeated addition, but string addition in a loop is O(n²) due to the creation of a new intermediate string at every step. The sum function is designed for numeric accumulation and its Python-level loop over parts incurs function call overhead for each addition, making it both conceptually wrong and significantly slower than str.join. Moreover, using sum with strings can be misleading to readers and is explicitly discouraged in Python's documentation.
- ✗
result = str.concat(*parts)
Why it's wrong here
str.concat(*parts) fails because Python's built-in str type does not define a concat method; strings do have a __add__ method for concatenation, but no concat. Attempting to call str.concat would raise AttributeError: type object 'str' has no attribute 'concat'. The *parts unpacking is also unnecessary and would only be useful if such a method existed and accepted multiple arguments, but the core issue is that the method does not exist in the standard library.
- ✗
result = ''; for part in parts: result += part
Why it's wrong here
Initializing an empty string and using a for loop with the += operator performs iterative concatenation, which creates a new string object for each iteration because strings are immutable. This results in O(n²) time complexity driven by repeated copying of all previously accumulated characters, causing noticeable slowdowns for large lists. While acceptable for a tiny number of parts, this pattern defeats the purpose of efficient bulk string building and is the classic anti-pattern that str.join is designed to replace.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCAP question from scratch — 169 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way this is tested on PCAP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A developer is building a large string by concatenating many substrings in a loop using '+'. What is the main performance issue?
hard- ✓ A.Each concatenation creates a new string object, leading to quadratic time complexity
- B.String concatenation is not allowed in loops
- C.Strings are immutable, so concatenation is impossible
- D.The '+' operator works only for characters, not strings
Why A: In Python, strings are immutable, so the '+' operator does not modify an existing string but creates a new string object each time it is used. In a loop, this results in O(n²) time complexity because each concatenation copies the entire accumulated string, making it highly inefficient for large or many substrings.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.