π¬ Question: Given a Boolean value True
or False
. How to convert it to a string "True"
or "False"
in Python?
Note that this tutorial doesn’t concern “concatenating a Boolean to a string”. If you want to do this, check out our in-depth article on the Finxter blog.
Simple Bool to String Conversion
To convert a given Boolean value to a string in Python, use the str(boolean)
function and pass the Boolean value into it. This converts Boolean True
to string "True"
and Boolean False
to string "False"
.
Here’s a minimal example:
>>> str(True) 'True' >>> str(False) 'False'
Python Boolean Type is Integer
Booleans are represented by integers in Python, i.e., bool
is a subclass of int
. Boolean value True
is represented with integer 1
. And Boolean value False
is represented with integer 0
.
Here’s a minimal example:
>>> True == 1 True >>> False == 0 True
Convert True to ‘1’ and False to ‘0’
To convert a Boolean value to a string '1'
or '0'
, use the expression str(int(boolean))
. For instance, str(int(True))
returns '1'
and str(int(False))
returns '0'
. This is because of Python’s use of integers to represent Boolean values.
Here’s a minimal example:
>>> str(int(True)) '1' >>> str(int(False)) '0'
Convert List of Boolean to List of Strings
To convert a Boolean to a string list, use the list comprehension expression [str(x) for x in my_bools]
assuming the Boolean list is stored in variable my_bools
. This converts each Boolean x
to a string using the built-in str()
function and repeats it for all x
in the Boolean list.
Here’s a simple example:
my_bools = [True, True, False, False, True] my_strings = [str(x) for x in my_bools] print(my_strings) # ['True', 'True', 'False', 'False', 'True']
Convert String Back to Boolean
What if you want to convert the string representation 'True'
and 'False'
(or: '1'
and '0'
) back to the Boolean representation True
and False
?
π Recommended Tutorial: String to Boolean Conversion
Here’s the short summary:
You can convert a string value s
to a Boolean value using the Python function bool(s)
.
For example, bool('True')
and bool('1')
return True
.
However, bool('False')
and bool('0')
return False
as well which may come unexpected to you.
π‘ This is because all Python objects are “truthy”, i.e., they have an associated Boolean value. As a rule of thumb: empty values return Boolean True
and non-empty values return Boolean False
. So, only bool('')
on the empty string ''
returns False
. All other strings return True
!
You can see this in the following example:
>>> bool('True') True >>> bool('1') True >>> bool('2') True >>> bool('False') True >>> bool('0') True >>> bool('') False
Okay, what to do about it?
Easy – first pass the string into the eval()
function and then pass the result into the bool()
function. In other words, the expression bool(eval(my_string))
converts a string to a Boolean mapping 'True'
and '1'
to Boolean True
and 'False'
and '0'
to Boolean False
.
Finally – this behavior is as expected by many coders just starting out.
Here’s an example:
>>> bool(eval('False')) False >>> bool(eval('0')) False >>> bool(eval('True')) True >>> bool(eval('1')) True
Feel free to go over our detailed guide on the function:
π Recommended Tutorial: Python eval()
deep dive