Python error
TypeError
string formatting
Python placeholders
programming debugging

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

python
# This causes the error
message = "Hello, {0}" % "Alice"
# TypeError: not all arguments converted during string formatting

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:

python
1# Fix 1: Use str.format() with {0} placeholders
2message = "Hello, {0}".format("Alice")
3print(message)  # "Hello, Alice"
4
5# Fix 2: Use % with %s placeholders
6message = "Hello, %s" % "Alice"
7print(message)  # "Hello, Alice"
8
9# Fix 3: Use f-string (Python 3.6+, recommended)
10name = "Alice"
11message = f"Hello, {name}"
12print(message)  # "Hello, Alice"

The Three Formatting Methods

1. % Formatting (Old Style)

python
1# Uses %s, %d, %f placeholders
2name = "Alice"
3age = 30
4print("Name: %s, Age: %d" % (name, age))
5# "Name: Alice, Age: 30"
6
7# Single value — no tuple needed
8print("Hello, %s" % name)
9
10# Multiple values — must use a tuple
11print("Name: %s, Age: %d" % (name, age))
12
13# Named placeholders with dict
14print("%(name)s is %(age)d" % {"name": "Alice", "age": 30})

2. str.format() (New Style)

python
1# Uses {}, {0}, {name} placeholders
2print("Name: {}, Age: {}".format("Alice", 30))
3print("Name: {0}, Age: {1}".format("Alice", 30))
4print("Name: {name}, Age: {age}".format(name="Alice", age=30))
5
6# Reuse arguments
7print("{0} said {0} likes {1}".format("Alice", "Python"))
8# "Alice said Alice likes Python"

3. f-strings (Python 3.6+)

python
1name = "Alice"
2age = 30
3
4# Inline expressions
5print(f"Name: {name}, Age: {age}")
6print(f"Next year: {age + 1}")
7print(f"Uppercase: {name.upper()}")
8print(f"Pi: {3.14159:.2f}")

Common Causes of the Error

Cause 1: Using {0} with %

python
1# WRONG — mixing str.format() placeholders with % operator
2msg = "Hello, {0}" % "Alice"  # TypeError
3
4# FIX
5msg = "Hello, {0}".format("Alice")

Cause 2: Wrong Number of Arguments

python
1# WRONG — one placeholder but two arguments
2msg = "Hello, %s" % ("Alice", "Bob")
3# TypeError: not all arguments converted during string formatting
4
5# FIX — use two placeholders
6msg = "Hello, %s and %s" % ("Alice", "Bob")
7
8# Or use a single argument
9msg = "Hello, %s" % "Alice"

Cause 3: Using % with a String That Has No Placeholders

python
1# WRONG — no %s placeholder
2msg = "Hello" % "Alice"  # TypeError
3
4# FIX — add a placeholder or use concatenation
5msg = "Hello, %s" % "Alice"
6msg = "Hello " + "Alice"

Cause 4: Modulo Operator on String

python
1# WRONG — accidentally using % as modulo on a string
2result = "100" % 3  # TypeError — % on string means formatting, not modulo
3
4# FIX — convert to int first
5result = int("100") % 3  # 1

Format Specifiers

Each method has its own syntax for formatting numbers, padding, and alignment:

python
1value = 3.14159
2
3# % style
4print("Pi: %.2f" % value)        # "Pi: 3.14"
5print("Padded: %10.2f" % value)   # "Padded:       3.14"
6
7# str.format()
8print("Pi: {:.2f}".format(value))       # "Pi: 3.14"
9print("Padded: {:10.2f}".format(value))  # "Padded:       3.14"
10
11# f-string
12print(f"Pi: {value:.2f}")         # "Pi: 3.14"
13print(f"Padded: {value:10.2f}")   # "Padded:       3.14"

Which to Use?

MethodWhen to use
f-stringsDefault choice for Python 3.6+. Fastest, most readable.
str.format()When the template is a variable (not a literal string)
% formattingLegacy code. Avoid in new code.
python
1# f-string is best for most cases
2name, age = "Alice", 30
3print(f"{name} is {age}")
4
5# str.format() when template comes from a variable or config
6template = "Hello, {name}!"
7print(template.format(name="Alice"))
8
9# % formatting — only in legacy code
10print("Hello, %s" % "Alice")

Common Pitfalls

  • Mixing % and {}: This is the #1 cause of the error. "{}" % value always 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 %s placeholders: "Hello, %s" % "Alice"
  • Use f-strings for the cleanest, fastest approach: f"Hello, {name}"
  • Never mix placeholder styles — "{}" % value and "%s".format(value) both cause errors

Course illustration
Course illustration

All Rights Reserved.