Why do I get TypeError not all arguments converted during string formatting trying to substitute a placeholder like 0 using ?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The error TypeError: not all arguments converted during string formatting occurs in Python when you mix up string formatting methods. Specifically, it happens when you use the % operator with str.format()-style placeholders like {0} or {}, or when you pass the wrong number of arguments to %-style formatting. Understanding the three string formatting systems in Python — % formatting, str.format(), and f-strings — and when to use each prevents this error.
The Error
The problem: {0} is str.format() syntax, but % is the old-style formatting operator. Python tries to interpret {0} as a %-format code, fails, and raises the error.
The Fix
Use the correct formatting method for your placeholder syntax:
The Three Formatting Methods
1. % Formatting (Old Style)
2. str.format() (New Style)
3. f-strings (Python 3.6+)
Common Causes of the Error
Cause 1: Using {0} with %
Cause 2: Wrong Number of Arguments
Cause 3: Using % with a String That Has No Placeholders
Cause 4: Modulo Operator on String
Format Specifiers
Each method has its own syntax for formatting numbers, padding, and alignment:
Which to Use?
| Method | When to use |
| f-strings | Default choice for Python 3.6+. Fastest, most readable. |
str.format() | When the template is a variable (not a literal string) |
% formatting | Legacy code. Avoid in new code. |
Common Pitfalls
- Mixing
%and{}: This is the #1 cause of the error."{}" % valuealways fails. Match your placeholder style to your formatting method. - Single vs tuple with
%:"Hello, %s" % ("Alice",)works (single-element tuple), but"Hello, %s" % ("Alice")also works (parentheses are optional for one value). For multiple values, a tuple is required:"%s %s" % ("A", "B"). - Dict with
%: When using%with a dict, all placeholders must be named:"%(name)s" % {"name": "Alice"}. Mixing named and positional placeholders fails. - f-string with backslashes: You cannot use backslashes inside f-string expressions:
f"{'\n'.join(items)}"is a syntax error in Python < 3.12. Use a variable:sep = '\n'; f"{sep.join(items)}". - Performance: f-strings are compiled at parse time and are faster than both
%and.format(). For performance-critical string building, prefer f-strings.
Summary
- The error occurs when you use
{0}or{}placeholders with the%formatting operator - Use
str.format()with{}placeholders:"Hello, {}".format("Alice") - Use
%with%splaceholders:"Hello, %s" % "Alice" - Use f-strings for the cleanest, fastest approach:
f"Hello, {name}" - Never mix placeholder styles —
"{}" % valueand"%s".format(value)both cause errors

