- Boolean in python format string
- # Print boolean values in Python
- # Converting a value to a boolean and printing the result
- # Printing a boolean value in a String
- # Passing multiple, comma-separated arguments to print()
- # Additional Resources
- Different ways to convert one boolean to string in python
- Convert bool to String in Python
- Using the str() function to convert bool to string in Python.
- Python print Boolean | Example code
- Print a boolean With String
- Print Boolean values of different data types in Python
- Print bool value based on expression outcome
Boolean in python format string
Last updated: Feb 20, 2023
Reading time · 3 min
# Print boolean values in Python
Use the print() function to print a boolean value in Python, e.g. print(my_bool) .
If the value is not of type boolean, use the bool() class to convert it to a boolean and print the result, e.g. bool(0) .
Copied!my_bool = True # ✅ print boolean value print(my_bool) # 👉️ True # ✅ print boolean value in a string print(f'is subscribed: my_bool>') # 👉️ is subscribed: True # ✅ print integer representation of boolean value print(int(True)) # 👉️ 1 print(int(False)) # 👉️ 0 # ✅ print boolean value converted to lowercase print(f'my_bool>'.lower()) # 👉️ true # ✅ Convert a value to a boolean print(bool('hello')) # 👉️ True print(bool(0)) # 👉️ False
We used the print() function to print boolean values.
The print function takes one or more objects and prints them to sys.stdout .
If you have a boolean value, you can pass it directly to the print() function to print it.
Copied!my_bool = True print(my_bool) # 👉️ True print(False) # 👉️ False
Note that the print() function returns None , so don’t try to store the result of calling print in a variable.
Copied!my_bool = True # ⛔️ BAD (print always returns None) result = print(f'is subscribed: my_bool>') print(result) # 👉️ None
Instead, store the value in a variable and pass the variable to the print() function.
Copied!my_bool = True result = f'is subscribed: my_bool>' print(result) # 👉️ is subscribed: True
If you aren’t sure what type a variable stores, use the built-in type() class.
Copied!my_bool = False print(type(my_bool)) # 👉️ print(isinstance(my_bool, bool)) # 👉️ True my_str = 'hello' print(type(my_str)) # 👉️ print(isinstance(my_str, str)) # 👉️ 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.
# Converting a value to a boolean and printing the result
If you need to convert a value to a boolean and print the result, use the bool() class.
Copied!print(bool('hello')) # 👉️ True print(bool(0)) # 👉️ False
The bool class converts truthy values to True and falsy values to False .
All values that are not truthy are considered falsy. The falsy values in Python are:
- constants defined to be falsy: None and False .
- 0 (zero) of any numeric type.
- empty sequences and collections: «» (empty string), () (empty tuple), [] (empty list), <> (empty dictionary), set() (empty set), range(0) (empty range).
# Printing a boolean value in a String
If you need to print a boolean value or a variable storing a boolean value in a string, use a formatted string literal.
Copied!my_bool = True # ✅ print boolean value in a string print(f'is subscribed: my_bool>') # 👉️ is subscribed: True
Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f .
Make sure to wrap expressions in curly braces — .
# Passing multiple, comma-separated arguments to print()
Alternatively, you can pass multiple, comma-separated arguments to the print() function.
Copied!# 👇️ is subscribed: True print('is subscribed:', True) # 👇️ is subscribed:True print('is subscribed:', True, sep='')
By default, when you pass multiple, comma-separated arguments to the print() function, they get separated by a space.
You can set the sep keyword argument to an empty string to remove the separator.
If you need to convert a boolean value to its integer representation, use the int() class.
Copied!print(int(True)) # 👉️ 1 print(int(False)) # 👉️ 0
The int class returns an integer object constructed from the provided number or string argument.
Conversely, if you need to convert an integer to a boolean value, use the bool() class.
Copied!print(bool(1)) # 👉️ True print(bool(0)) # 👉️ False
The bool() class takes a value and converts it to a boolean (True or False). If the provided value is falsy or omitted, then bool returns False , otherwise it returns True .
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
Different ways to convert one boolean to string in python
Boolean values are True and False. Sometimes we may need to convert these values to string. In python, we have different ways to do that and in this post, I will show you three different ways to convert one boolean value to string in python.
Using format, we can format one boolean value to string. For example :
= True bool_false = False print('bool_true = <>'.format(bool_true)) print('bool_false = <>'.format(bool_false))
%s is used to format values into string. We can use this to format boolean values to string as like below :
= True bool_false = False print('bool_true = %s'%bool_true) print('bool_false = %s'%bool_false)
It will print the same output.
Another way to convert one boolean to string in python :
= True bool_false = False print('bool_true = '+str(bool_true)) print('bool_false = '+str(bool_false))
It will print the same output.
All methods give the same output. You can use any one of them.
Convert bool to String in Python
In this post, we will see how to convert bool to string in Python.
Before moving on to finding the different methods to implement the task of converting bool to string in Python, let us first understand what a boolean and a string are.
What is a string in Python?
A string is the most versatile and the used data type in Python, which is utilized to represent any of the Unicode characters in a variable. We can make the use of double quotes or single quotes to declare a string. Moreover, a string is flexible, meaning it is capable of holding any value.
Here, we show a simple example of how to declare a string.
What is a bool in Python?
Boolean or simply bool is one of the data types available to use in Python and is capable of returning either the value True or False . This is a unique property that makes this data type ideal to use in problem statements. A bool value is utilized while implementing many of the conditional statements and iteration statements and is considered to be an essential data type in Python.
Using the str() function to convert bool to string in Python.
The in-built str() function simply converts any specified value taken as its argument and returns a string. The value taken in could be any variable, but it can even be any direct value, for example, the number 9 , if taken as an argument, will directly be converted to a string value.
The following code uses the str() function to convert bool to string in Python.
Python print Boolean | Example code
You can simply print the Boolean value in python. See below code:
Output: True
Print a boolean With String
If you try the print Boolean value with the string it will throw an error.
bol = True print("Hello "+ bol)
Output: can only concatenate str (not “bool”) to str
bol = True print("Hello " + str(bol))
bol = True print("Hello ", bol)
Print Boolean values of different data types in Python
Use the built-in bool() method to print the value of a variable.
test = [] print(test, 'is', bool(test)) test = [0] print(test, 'is', bool(test)) test = 0.0 print(test, 'is', bool(test)) test = None print(test, 'is', bool(test)) test = True print(test, 'is', bool(test)) test = 'Easy string' print(test, 'is', bool(test))
Print bool value based on expression outcome
print(1 > 9) print(1 == 9) print(1 < 9)
Do comment if you have any doubts and suggestions on this Python Boolean topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.