Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert Bool (True/False) to a String in Python?
1
How to Convert Bool (True/False) to a String in Python?

5/5 – (1 vote)

[Image: 1f4ac.png] Question: Given a Boolean value
Code:
True
or
Code:
False
. How to convert it to a string
Code:
"True"
or
Code:
"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
Code:
str(boolean)
function and pass the Boolean value into it. This converts Boolean
Code:
True
to string
Code:
"True"
and Boolean
Code:
False
to string
Code:
"False"
.

Here’s a minimal example:

Code:
>>> str(True) 'True' >>> str(False) 'False'
Python Boolean Type is Integer

Booleans are represented by integers in Python, i.e.,
Code:
bool
is a subclass of
Code:
int
. Boolean value
Code:
True
is represented with integer
Code:
1
. And Boolean value
Code:
False
is represented with integer .

Here’s a minimal example:

Code:
>>> True == 1 True >>> False == 0 True
Convert True to ‘1’ and False to ‘0’

To convert a Boolean value to a string
Code:
'1'
or
Code:
'0'
, use the expression
Code:
str(int(boolean))
. For instance,
Code:
str(int(True))
returns
Code:
'1'
and
Code:
str(int(False))
returns
Code:
'0'
. This is because of Python’s use of integers to represent Boolean values.

Here’s a minimal example:

Code:
>>> 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
Code:
[str(x) for x in my_bools]
assuming the Boolean list is stored in variable
Code:
my_bools
. This converts each Boolean
Code:
x
to a string using the built-in
Code:
str()
function and repeats it for all
Code:
x
in the Boolean list.

Here’s a simple example:

Code:
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
Code:
'True'
and
Code:
'False'
(or:
Code:
'1'
and
Code:
'0'
) back to the Boolean representation
Code:
True
and
Code:
False
?

[Image: 1f449.png] Recommended Tutorial: String to Boolean Conversion

Here’s the short summary:

You can convert a string value
Code:
s
to a Boolean value using the Python function
Code:
bool(s)
.

For example,
Code:
bool('True')
and
Code:
bool('1')
return
Code:
True
.

However,
Code:
bool('False')
and
Code:
bool('0')
return
Code:
False
as well which may come unexpected to you.

[Image: 1f4a1.png] 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
Code:
True
and non-empty values return Boolean
Code:
False
. So, only
Code:
bool('')
on the empty string
Code:
''
returns
Code:
False
. All other strings return
Code:
True
!

You can see this in the following example:

Code:
>>> 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
Code:
eval()
function and then pass the result into the
Code:
bool()
function. In other words, the expression
Code:
bool(eval(my_string))
converts a string to a Boolean mapping
Code:
'True'
and
Code:
'1'
to Boolean
Code:
True
and
Code:
'False'
and
Code:
'0'
to Boolean
Code:
False
.

Finally – this behavior is as expected by many coders just starting out.

Here’s an example:

Code:
>>> 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:

[Image: 1f449.png] Recommended Tutorial: Python
Code:
eval()
deep dive


[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: