Concatenate a boolean to a string in Python

avatar

Borislav Hadzhiev

Last updated: Aug 28, 2022

banner

Photo from Unsplash

Concatenate a boolean to a string in Python #

To concatenate a boolean to a string:

  1. Use the str() class to convert the boolean to a string.
  2. Use the addition (+) operator to concatenate the two strings.
  3. The result will be the concatenation of the two values.
main.py
my_str = 'Is subscribed: ' my_bool = False # ✅ convert boolean to string result = my_str + str(my_bool) print(result) # 👉 Is subscribed: False # ------------------------------ # ✅ using formatted string literal result = f'{my_str}{my_bool}' print(result) # 👉️ Is subscribed: False # ------------------------------ # ✅ using str.format() method result = '{}{}'.format(my_str, my_bool) print(result) # 👉️ Is subscribed: False

The str class takes an object as an argument and returns the str version of the object.

We used the str class to convert the boolean to a string because we cannot use the addition operator with a string and a boolean.

main.py
my_str = 'Is subscribed: ' my_bool = False # ⛔️ TypeError: can only concatenate str (not "bool") to str result = my_str + my_bool
The values on the left and right-hand sides of the addition (+) operator need to be of compatible types, so we have to convert the boolean to a string.
main.py
my_str = 'Is subscribed: ' my_bool = False result = my_str + str(my_bool) print(result) # 👉 Is subscribed: False

If you aren't sure what type a variable stores, use the built-in type() class.

main.py
my_str = 'is subscribed:' print(type(my_str)) # 👉️ <class 'str'> print(isinstance(my_str, str)) # 👉️ True my_bool = True print(type(my_bool)) # 👉️ <class 'bool'> print(isinstance(my_bool, bool)) # 👉️ True

The type class returns the type of an object.

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

An alternative approach is to use a formatted string literal.

Concatenate a boolean to a string using formatted string literal #

Use a formatted string literal to concatenate a boolean to a string, e.g. result = f'{my_str}{my_bool}'. By prefixing the string with f, we can include expressions and evaluate variables by wrapping them in curly braces.

main.py
my_str = 'Is subscribed: ' my_bool = False result = f'{my_str}{my_bool}' print(result) # 👉️ Is subscribed: False
When using formatted string literals, we don't have to explicitly convert the boolean to a string. The conversion is done for us automatically.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

main.py
my_bool_1 = True my_bool_2 = False result = f'{my_bool_1} is the opposite of {my_bool_2}' print(result) # 👉️ True is the opposite of False

Make sure to wrap expressions in curly braces - {expression}.

Alternatively, you can use the str.format() method.

Concatenate a boolean to a string using str.format() #

Use the str.format() method to concatenate a boolean to a string, e.g. result = '{}{}'.format(my_str, my_bool). The str.format() method enables us to include replacement fields and perform string formatting operations.

main.py
my_str = 'Is subscribed: ' my_bool = True result = '{}{}'.format(my_str, my_bool) print(result) # 👉️ Is subscribed: True

The str.format method performs string formatting operations.

The string the method is called on can contain replacement fields specified using curly braces {}.

Make sure to provide exactly as many arguments to the format() method as you have replacement fields in the string.

Which approach you pick is a matter of personal preference. I'd go with using a formatted string literal because they don't require conversion and allow insertion of expressions directly in the string.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.