Converting a String to a Boolean in Python
In this article, we will discuss how to use the below functions to convert a string to a boolean in Python.
Using the Bool function:
The bool()
function takes an argument and returns a boolean value based on the argument passed. If the argument is empty or evaluates to False
, the function returns False
. Otherwise, it returns True
. Let's see some examples to understand how the bool()
function works.
>>> bool('')
False
>>> bool('0')
True
>>> bool('1')
True
>>> bool('False')
True
>>> bool('True')
True
As you can see from the examples above, the bool()
function returns False
only when the argument is empty or evaluates to False
. Otherwise, it returns True
.
Now that we have understood how the bool()
function works, let's see how to use it to convert a string to a boolean in Python.
s = 'True'
b = bool(s)
print(b)
In the example above, we have assigned a string 'True'
to the variable s
. We then pass this variable as an argument to the bool()
function and assign the returned boolean value to the variable b
. Finally, we print the value of b
, which will be True
.
Let's see another example:
s = 'False'
b = bool(s)
print(b)
In this example, we have assigned a string 'False'
to the variable s
. We then pass this variable as an argument to the bool()
function and assign the returned boolean value to the variable b
. Finally, we print the value of b
, which will be True
.
Wait, why did the bool()
function return True
even though the string 'False'
was passed as an argument? This is because any non-empty string (except for '0'
) will evaluate to True
when passed to the bool()
function. Therefore, the string 'False'
evaluates to True
.
Using If Else Statement
So how do we handle cases where we want to convert a string to a boolean based on its actual value? One way to do this is to use an if
statement to check the value of the string and assign the boolean value accordingly.
s = 'True'
if s == 'True':
b = True
else:
b = False
print(b)
In the example above, we have used an if
statement to check if the value of the string s
is 'True'
. If it is, we assign True
to the variable b
. Otherwise, we assign False
. Finally, we print the value of b
, which will be True
.
We can modify the above example to handle the string 'False'
as well:
s = 'False'
if s == 'True':
b = True
else:
b = False
print(b)
In this example, we have checked if the value of the string s
is 'True'
. Since it is not, we assign False
to the variable b
. Finally, we print the value of b
, which will be False
.
While the above method works, it is not very efficient when dealing with multiple strings that need to be converted to booleans. In such cases, we can use a dictionary to map the string values to their boolean equivalents.
s = 'True'
d = {'True': True,'False': False}
b = d.get(s)
print(b)
In the example above, we have created a dictionary 'd
' that maps the string values 'True
' and 'False'
to their boolean equivalents. We then use the `get()` method of the dictionary to retrieve the boolean value corresponding to the string value `s`. Finally, we assign the retrieved boolean value to the variable `b` and print its value.
Using dictionary:
Using a dictionary to map string values to boolean values is a more efficient way of converting multiple strings to booleans. It also makes the code easier to read and maintain.
There is another way to convert a string to a boolean in Python, which is by using the 'ast' (Abstract Syntax Trees) module. The 'ast.literal_eval()'
function can be used to safely evaluate strings containing Python expressions, including boolean literals.
import ast
s = 'True'
b = ast.literal_eval(s)
print(b)
In the example above, we have imported the 'ast'
module and used the 'literal_eval()'
function to safely evaluate the string 's'. The function returns the boolean value corresponding to the string `s`, which we assign to the variable `b`. Finally, we print the value of `b`.
The 'ast.literal_eval()'
function is useful when you want to evaluate strings that contain Python expressions other than boolean literals as well.
Comments (0)