String formatting with booleans in Python

avatar

Borislav Hadzhiev

Last updated: Aug 30, 2022

banner

Photo from Unsplash

String formatting with booleans in Python #

Use a formatted string literal to perform string formatting with booleans, e.g. result = f'Subscribe to newsletter: {checkbox_checked}'. Formatted string literals let us include expressions and variables inside of a string by prefixing the string with f.

main.py
checkbox_checked = True # ✅ string formatting with booleans (f-string) result = f'Subscribe to newsletter: {checkbox_checked}' print(result) # 👉️ Subscribe to newsletter: True # ---------------------------------- # ✅ string formatting with booleans (addition operator) result = 'Subscribe to newsletter: ' + str(checkbox_checked) print(result) # 👉️ Subscribe to newsletter: True # ---------------------------------- # ✅ string formatting with booleans with conditionals result = f'Subscribe to newsletter: {"yes" if checkbox_checked else "no"}' print(result) # 👉️ Subscribe to newsletter: yes

The first example uses a formatted string literal.

main.py
checkbox_checked = True result = f'Subscribe to newsletter: {checkbox_checked}' print(result) # 👉️ Subscribe to newsletter: True

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

main.py
bool1 = True bool2 = False my_str = 'Subscribe to newsletter:' result = f'{my_str} {bool1}/{bool2}' print(result) # Subscribe to newsletter: True/False

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

Formatted string literals automatically take care of converting boolean values to strings.

Note that when using the addition (+) operator, we have to explicitly convert the boolean values to strings.

main.py
checkbox_checked = True result = 'Subscribe to newsletter: ' + str(checkbox_checked) print(result) # 👉️ Subscribe to newsletter: True

The values of the left and right-hand sides of the addition (+) operator have to be of compatible types, so we used the str() class to convert the boolean to a string.

If you need to perform conditional string formatting with booleans, use an f-string with the ternary operator.

main.py
checkbox_checked = True result = f'Subscribe to newsletter: {"yes" if checkbox_checked else "no"}' print(result) # 👉️ Subscribe to newsletter: yes

The ternary operator is very similar to an if/else statement.

The example checks if the checkbox_checked variable stores a truthy value and if it does, the string yes is returned, otherwise, the else statement returns no.

Note that we wrapped the f-string in single quotes and used double quotes to wrap the strings in the expressions.

Had we used single quotes in the expressions as well, we would have terminated the f-string prematurely.

You can also use the str.format() method to format strings with booleans.

main.py
checkbox_checked = True result = 'Subscribe to newsletter: {}'.format(checkbox_checked) print(result) # 👉️ Subscribe to newsletter: True

The str.format method performs string formatting operations.

main.py
bool1 = True bool2 = False result = 'Receive notifications: {}/{}'.format(bool1, bool2) print(result) # 👉️ Receive notifications: True/False

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.

Here is an example that uses the str.format() method with a condition.

main.py
checkbox_checked = True result = 'Subscribe to newsletter: {}'.format( 'yes' if checkbox_checked else 'no') print(result) # 👉️ Subscribe to newsletter: yes

If the boolean stores a True value, we return the string yes, otherwise the string no is returned.

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.