10-04-2022, 11:37 AM
How to Convert Bool (True/False) to a String in Python?
5/5 – (1 vote)
Question: Given a Boolean value or . How to convert it to a string or 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 function and pass the Boolean value into it. This converts Boolean to string and Boolean to string .
Here’s a minimal example:
Python Boolean Type is Integer
Booleans are represented by integers in Python, i.e., is a subclass of . Boolean value is represented with integer . And Boolean value is represented with integer .
Here’s a minimal example:
Convert True to ‘1’ and False to ‘0’
To convert a Boolean value to a string or , use the expression . For instance, returns and returns . This is because of Python’s use of integers to represent Boolean values.
Here’s a minimal example:
Convert List of Boolean to List of Strings
To convert a Boolean to a string list, use the list comprehension expression assuming the Boolean list is stored in variable . This converts each Boolean to a string using the built-in function and repeats it for all in the Boolean list.
Here’s a simple example:
Convert String Back to Boolean
What if you want to convert the string representation and (or: and ) back to the Boolean representation and ?
Recommended Tutorial: String to Boolean Conversion
Here’s the short summary:
You can convert a string value to a Boolean value using the Python function .
For example, and return .
However, and return 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 and non-empty values return Boolean . So, only on the empty string returns . All other strings return !
You can see this in the following example:
Okay, what to do about it?
Easy – first pass the string into the function and then pass the result into the function. In other words, the expression converts a string to a Boolean mapping and to Boolean and and to Boolean .
Finally – this behavior is as expected by many coders just starting out.
Here’s an example:
Feel free to go over our detailed guide on the function:
Recommended Tutorial: Python deep dive
5/5 – (1 vote)
Question: Given a Boolean value Code:
TrueCode:
FalseCode:
"True"Code:
"False"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)Code:
TrueCode:
"True"Code:
FalseCode:
"False"Here’s a minimal example:
Code:
>>> str(True) 'True'
>>> str(False) 'False'Booleans are represented by integers in Python, i.e.,
Code:
boolCode:
intCode:
TrueCode:
1Code:
FalseHere’s a minimal example:
Code:
>>> True == 1
True
>>> False == 0
TrueTo convert a Boolean value to a string
Code:
'1'Code:
'0'Code:
str(int(boolean))Code:
str(int(True))Code:
'1'Code:
str(int(False))Code:
'0'Here’s a minimal example:
Code:
>>> str(int(True)) '1'
>>> str(int(False)) '0'To convert a Boolean to a string list, use the list comprehension expression
Code:
[str(x) for x in my_bools]Code:
my_boolsCode:
xCode:
str()Code:
xHere’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']What if you want to convert the string representation
Code:
'True'Code:
'False'Code:
'1'Code:
'0'Code:
TrueCode:
False
Recommended Tutorial: String to Boolean ConversionHere’s the short summary:
You can convert a string value
Code:
sCode:
bool(s)For example,
Code:
bool('True')Code:
bool('1')Code:
TrueHowever,
Code:
bool('False')Code:
bool('0')Code:
False
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:
TrueCode:
FalseCode:
bool('')Code:
''Code:
FalseCode:
TrueYou can see this in the following example:
Code:
>>> bool('True')
True
>>> bool('1')
True
>>> bool('2')
True
>>> bool('False')
True
>>> bool('0')
True
>>> bool('')
FalseOkay, what to do about it?
Easy – first pass the string into the
Code:
eval()Code:
bool()Code:
bool(eval(my_string))Code:
'True'Code:
'1'Code:
TrueCode:
'False'Code:
'0'Code:
FalseFinally – 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'))
TrueFeel free to go over our detailed guide on the function:
Recommended Tutorial: Python Code:
eval()
