A Python TypeError during string formatting usually means an operation received a value of the wrong type. A common example is trying to concatenate a string and an integer with +. Modern Python gives you several clean ways to include numbers and other values in text without manually converting every value first.
Fix string-and-number concatenation errors
This raises a TypeError:
age = 42
message = "Age: " + age
You can convert the value explicitly:
message = "Age: " + str(age)
For most new code, an f-string is easier to read:
age = 42
message = f"Age: {age}"
The expression inside the braces is evaluated and formatted as part of the string, so an integer does not have to be concatenated with the surrounding text.
Format numbers instead of converting them blindly
F-strings also let you control how a value appears:
price = 19.5
quantity = 3
print(f"Price: ${price:.2f}")
print(f"Quantity: {quantity:d}")
.2f displays a floating-point value with two digits after the decimal point. The d format expects an integer. If you pass an incompatible value to a format specification, Python can still raise an error, so formatting does not eliminate the need to understand your data types.
Inspect the value that is causing the error
When the problem is not obvious, print or inspect type(value). Data from JSON, CSV files, databases, forms, and command-line arguments may not have the type you expect. Fixing the data at the point where it enters your program is often better than scattering str() calls throughout the output code.
Use repr when debugging ambiguous strings
repr(value), or the !r conversion in an f-string, can reveal quotes, escape characters, and other details that ordinary string output hides:
value = "hello\nworld"
print(f"value={value!r}")
A practical TypeError troubleshooting sequence
- Read the final line of the traceback to identify the operation that failed.
- Inspect the values involved and check each one with
type()when the type is not obvious. - Decide whether the value should be converted, validated, or formatted differently.
- Prefer an f-string or another formatting method when the goal is to display a non-string value inside text.
- Run the code again with representative inputs, including empty values or unexpected data types that could reach the same line.
Do not catch TypeError just to hide it
A broad try/except TypeError around formatting code can conceal a real programming mistake. Read the traceback, identify the operation that received incompatible types, and correct the value or formatting expression. That produces code that is easier to maintain and less likely to fail somewhere else.

Kermit Matthews is a freelance writer based in Philadelphia, Pennsylvania with more than a decade of experience writing technology guides. He has a Bachelor’s and Master’s degree in Computer Science and has spent much of his professional career in IT management.
He specializes in writing content about iPhones, Android devices, Microsoft Office, and many other popular applications and devices.