Skip to main content

How to Resolve "TypeError: Can't Convert 'float' Object to str Implicitly" in Python

Encountering a TypeError when trying to combine a string with a number is one of the most common mistakes in Python, especially for beginners. The error TypeError: can only concatenate str (not "float") to str (or its Python 2 variant TypeError: can't convert 'float' object to str implicitly) occurs when you try to use the + operator to join a string with a float. In this guide, we'll explain why Python raises this error and walk through multiple ways to fix it with practical examples.

Why Does This Error Occur?​

Unlike some other programming languages, Python does not automatically convert (or "implicitly coerce") numbers to strings when you try to concatenate them. The + operator behaves differently depending on the types of its operands:

  • str + str → Concatenation: "hello" + " world" → "hello world"
  • float + float → Addition: 3.14 + 2.0 → 5.14
  • str + float → TypeError ❌

When Python sees str + float, it doesn't know whether you want string concatenation or arithmetic addition, so it raises an error rather than guessing.

❌ Example that triggers the error:

price = 19.99
message = "The total is: $" + price
print(message)

Output:

TypeError: can only concatenate str (not "float") to str
Python 2 vs Python 3

In Python 2, the error message reads: TypeError: can't convert 'float' object to str implicitly. In Python 3, it reads: TypeError: can only concatenate str (not "float") to str. The cause and fix are the same in both versions.

How to Fix It​

Solution 1: Convert the Float to a String with str()​

The most straightforward fix is to explicitly convert the float to a string using the built-in str() function:

price = 19.99
message = "The total is: $" + str(price)
print(message)

Output:

The total is: $19.99

This works, but it gives you no control over formatting (e.g., decimal places). For more polished output, use one of the methods below.

F-strings (formatted string literals) are the most readable and Pythonic way to embed values inside strings. They automatically handle type conversion:

price = 19.99
message = f"The total is: ${price}"
print(message)

Output:

The total is: $19.99

F-strings also support inline formatting for controlling decimal places, alignment, padding, and more:

price = 19.9
tax = 1.5983

print(f"Price: ${price:.2f}")
print(f"Tax: ${tax:.2f}")
print(f"Total: ${price + tax:.2f}")

Output:

Price: $19.90
Tax: $1.60
Total: $21.50

The :.2f format specifier means "format as a float with exactly 2 decimal places."

tip

F-strings are generally the best choice for string formatting in modern Python. They're concise, readable, and performant.

Solution 3: Use the format() Method​

The format() method uses {} placeholders within a string to insert values:

price = 14.1512
message = "The price is ${:.2f}".format(price)
print(message)

Output:

The price is $14.15

You can also use named or positional placeholders for more complex formatting:

item = "Widget"
price = 9.99
quantity = 3

message = "Item: {name} | Price: ${cost:.2f} | Qty: {qty}".format(
name=item, cost=price, qty=quantity
)
print(message)

Output:

Item: Widget | Price: $9.99 | Qty: 3

Solution 4: Use % Formatting (Legacy Style)​

The % operator is the oldest string formatting method in Python. While it's less common in modern code, you may encounter it in older projects:

price = 19.99
message = "The total is: $%.2f" % price
print(message)

Output:

The total is: $19.99

Common format specifiers:

SpecifierMeaningExample
%sString"Name: %s" % "Alice"
%fFloat (default 6 decimal places)"%.2f" % 3.14 → 3.14
%dInteger"%d" % 42 → 42
%eScientific notation"%e" % 1500 → 1.500000e+03

Common Scenarios Where This Error Appears​

Building Output Messages​

❌ Wrong:

temperature = 98.6
print("Current temperature: " + temperature + "°F")
# TypeError: can only concatenate str (not "float") to str

✅ Correct:

temperature = 98.6
print(f"Current temperature: {temperature}°F")

Output:

Current temperature: 98.6°F

Constructing File Paths or URLs​

❌ Wrong:

version = 3.11
path = "/usr/bin/python" + version
# TypeError: can only concatenate str (not "float") to str

✅ Correct:

version = 3.11
path = f"/usr/bin/python{version}"
print(path)

Output:

/usr/bin/python3.11

Logging or Debugging with Multiple Values​

❌ Wrong:

x = 5.0
y = 10.5
result = x * y
print("Result of " + x + " * " + y + " = " + result)
# TypeError: can only concatenate str (not "float") to str

✅ Correct:

x = 5.0
y = 10.5
result = x * y
print(f"Result of {x} * {y} = {result}")

Output:

Result of 5.0 * 10.5 = 52.5
info

Note that print() can also accept multiple arguments separated by commas, which automatically adds spaces between them and handles type conversion:

print("Result of", x, "*", y, "=", result)

This avoids the error entirely, though it gives you less control over formatting.

This Also Applies to Other Numeric Types​

The same error occurs with integers and other non-string types, not just floats:

count = 42
message = "There are " + count + " items." # TypeError!

The fix is identical: use str(), f-strings, or format():

count = 42
message = f"There are {count} items."
print(message)

Output:

There are 42 items.

Quick Reference: Solutions Summary​

MethodSyntaxPython Version
str() conversion"text" + str(3.14)All versions
f-stringsf"text {3.14}"3.6+
format() method"text {:.2f}".format(3.14)2.6+ / 3.0+
% formatting"text %.2f" % 3.14All versions
print() with commasprint("text", 3.14)All versions

Conclusion​

The TypeError: can't convert 'float' object to str implicitly error occurs because Python refuses to automatically convert a float to a string during concatenation.

The fix is to explicitly handle the conversion yourself. For modern Python code, f-strings are the recommended approach: they're readable, concise, and offer powerful formatting options like controlling decimal places with :.2f. For older codebases, str(), format(), or % formatting all work equally well.

Whichever method you choose, the key principle is the same: always ensure both sides of the + operator are the same type when concatenating strings.