How to Use sum() on a List of Strings in Python
You are a data analyst at a retail company. You have a list of sales figures stored as strings in a list: sales = ['100', '200', '300']. You need to calculate the total sum. A colleague suggests using: total = sum(sales). However, this raises a TypeError because sum() requires numeric values. Which approach should you take to correctly calculate the total as an integer?
Quick Answer
The answer is to use `total = sum(map(int, sales))`. This approach is correct because the `sum()` function in Python requires numeric operands; passing a list of strings directly, as in `sum(sales)`, raises a `TypeError` since Python cannot add strings numerically. By applying `map(int, sales)`, you convert each string element to an integer before summation, allowing `sum()` to compute the total as an integer. On the Certified Entry-Level Python Programmer PCEP exam, this question tests your understanding of type conversion and the `sum()` function’s strict requirement for numeric input—a common trap is assuming `sum()` can implicitly handle strings. The key insight is that `sum()` works on iterables of numbers, not strings, so you must explicitly convert using `map()` or a generator expression. Memory tip: think “map to int before sum” to avoid the TypeError trap.
⚠ Common exam trap
Python Institute often tests the misconception that `sum()` can implicitly convert strings to numbers, or that catching an exception is a valid workaround, when in fact explicit conversion via `int()` is required.
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
✓
Use total = sum(int(s) for s in sales)
Option B uses a generator expression to convert each string element to an integer before passing them to sum(). Option A attempts to catch the TypeError but doesn't convert the strings to numbers. Option C uses str() to convert the numbers to strings, so sum() still receives strings and raises a TypeError. Option D attempts to concatenate strings with +=, resulting in a string, not a numeric total.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Use total = sum(sales) and catch TypeError
Why it's wrong here
Catching TypeError does not perform the conversion; the sum is never calculated correctly.
- ✓
Use total = sum(int(s) for s in sales)
Why this is correct
Converts each string to int using a generator expression, then sums the integers. This is the correct approach.
- ✗
Use total = sum(map(str, sales))
Why it's wrong here
map(str, sales) converts each element back to a string, so sum() still receives strings and raises a TypeError.
- ✗
Use total = 0; for s in sales: total += s
Why it's wrong here
Concatenates strings (e.g., '100' + '200' = '100200'), not numeric addition.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 498-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way this is tested on PCEP
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 data analyst uses Python to process a CSV file containing sales data. The file has columns: 'Product', 'Price', 'Quantity'. The analyst writes a script to compute total sales: sum of Price * Quantity for each row. The code reads each row as a list of strings. The analyst uses: total = 0; for row in reader: total += row['Price'] * row['Quantity']; print(total). The script raises a TypeError. What is the best fix?
hard- A.Convert Price and Quantity to float before multiplication.
- B.Change the loop to for i in range(len(reader)): total += reader[i][1] * reader[i][2].
- C.Use integer multiplication and then convert to float.
- ✓ D.Use float(row[1]) * float(row[2]) instead of row['Price'] * row['Quantity'].
Why D: The TypeError occurs because row is a list of strings, so integer indices must be used instead of string keys. Multiplying strings also requires conversion. Option D correctly uses float(row[1]) * float(row[2]) with indices, fixing both issues. Option A only converts to float but still uses invalid string keys.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP 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 PCEP exam.