DEV Community

Cover image for How to fix "can only concatenate str (not “bool”) to str" in Python
Reza Lavarian
Reza Lavarian

Posted on • Originally published at decodingweb.dev

How to fix "can only concatenate str (not “bool”) to str" in Python

Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza

The Python error “TypeError: can only concatenate str (not “bool”) to str” occurs if you concatenate a string with a boolean value (True or False).

Here’s what the error looks like on Python 3:

Traceback (most recent call last):
 File "/dwd/sandbox/test.py", line 3, in 
  print('Not everything is ' + not_everything)
     ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~
TypeError: can only concatenate str (not "bool") to str
Enter fullscreen mode Exit fullscreen mode

Why does it happen?

Python as a Strongly-typed programming language doesn't allow some operations on specific data types. For instance, you can't add 2 to False (because it makes no sense!) - depending on the operation, the error messages might vary.

Now, if you try to concatenate a string literal with a boolean value, you'll get the "TypeError: can only concatenate str (not "bool") to str".

The following code tries to concatenate the string 'Not everything is ' with a boolean value (True)

# ⛔ Raises TypeError: can only concatenate str (not bool) to str
not_everything = True
print('Not everything is ' + not_everything)

## How to fix TypeError: can only concatenate str (not "bool") to str
Enter fullscreen mode Exit fullscreen mode

If you need to use the + operator, ensure the operands on either side are strings. Remember: birds of a feather flock together 🦜 + 🦜.

A quick fix would be converting the boolean value to a string value with the str() function.

But if you want to insert several boolean values into a string, you'll have several options:

  • Use an f-string
  • Use printf-style formatting
  • Use print() with multiple arguments - ideal for debugging
  • Use the str() function

Let's explore each method with examples.

Use an f-string: Formatted string literals (a.k.a f-strings) are a robust way of inserting boolean values into string literals. You create an f-string by prefixing it with f or F and writing expressions inside curly braces ({}):

not_everything = True

print(f'Not everything is {not_everything}')
# output: Not everything is True
Enter fullscreen mode Exit fullscreen mode

Use printf-style formatting: However, if you prefer to use the old-fashioned string formatting (string % values), you can do it like so:

not_everything = True

print('Not everything is %s' % not_everything)
# output: Not everything is True
Enter fullscreen mode Exit fullscreen mode

When using the old-style formatting, ensure the format string is valid. Otherwise, you'll get another type error: not all arguments converted during string formatting.

Use print() with multiple arguments - ideal for debugging: If you're concatenating a string with a boolean and readability isn't a concern, you can pass the string and the boolean value(s) as separate arguments to the print() function.

All the positional arguments passed to the print() function are automatically converted to strings - like how str() works.

not_everything = True

print('Not everything is', not_everything)
# output: Not everything is True
Enter fullscreen mode Exit fullscreen mode

The print() function outputs the arguments separated by a space. You can change the separator via the sep keyword argument.

Use the str() function: If you need to generate the string without printing it, use the str() function to convert the boolean value to a string value:

not_everything = True
output = 'Not everything is ' + str(not_everything)

print(output)
# output: Not everything is True
Enter fullscreen mode Exit fullscreen mode

Alright, I think that does it! I hope this quick guide helped you fix your problem.

Thanks for reading!

❤️ You might like:

Top comments (0)