In this python tutorial, you will learn how to convert a string to a boolean.
Table Of Contents
In Python, True and False are the two boolean values. So we will see 4 ways to convert strings to boolean values.
Convert a string to a boolean using bool()
Here, we will directly use the bool() method to convert a string to a boolean value. If the string is empty, then the returned boolean value is False, otherwise, it is True.
Syntax:
Frequently Asked:
bool(inp_str)
Parameter:
It takes inp_str as the only parameter, that refers to the actual input string.
Example 1
In this example, we will convert the string – “Welcome to thisPointer” to a boolean.
Latest Python - Video Tutorial
# Consider the string inp_str= "Welcome to thisPointer" print("Actual String: ",inp_str) print(type(inp_str)) # Convert to boolean string converted=bool(inp_str) print("Boolean value: ",converted) print(type(converted))
Output:
Actual String: Welcome to thisPointer <class 'str'> Boolean value: True <class 'bool'>
The string is converted to boolean – True, since it is not empty and also we displayed the class i.e. bool.
Example 2
In this example, we will convert the string – “” (empty) to boolean.
inp_str= "" print("Actual String: ",inp_str) print(type(inp_str)) # Convert to boolean string converted=bool(inp_str) print("Boolean Value: ",converted) print(type(converted))
Output:
Actual String: <class 'str'> Boolean Value: False <class 'bool'>
A string is converted to boolean – False, since it is empty and also we displayed the class i.e. bool.
Convert a string to a boolean using strtobool()
The strtobool() function available in the distutils.util module, and it is used to convert the string values to 1 or 0. Value 1 represents True and 0 represents False.
Here,
1. It can take 3 types of string values for True i.e. Positive values – “Yes”, “True”, or “On”. For these values, strtobool() return 1.
2. It can take 3 types of string values for False i.e. Negative values – No, False, and Off. For these values, strtobool() return 0.
The strtobool() function can take only one of the above mentioned values.
Syntax:
distutils.util.strtobool("Yes/No/True/False/On/Off")
Parameter:
It takes one of the above possible values as a parameter.
Example 1
In this example we will convert the strings -Yes, True, and On to a boolean value.
# Import util module import distutils from distutils import util # Convert Yes to boolean print(distutils.util.strtobool("Yes")) # Convert True to boolean print(distutils.util.strtobool("True")) # Convert On to boolean print(distutils.util.strtobool("On"))
Output:
1 1 1
Strings are converted to boolean – True.
Example 2
In this example, we will convert the strings -No, False and Off to boolean.
# Import util module import distutils from distutils import util # Convert No to boolean print(distutils.util.strtobool("No")) # Convert False to boolean print(distutils.util.strtobool("False")) # Convert Off to boolean print(distutils.util.strtobool("Off"))
Output:
0 0 0
Strings are converted to boolean – False.
Convert a string to a boolean using json.loads()
The json.loads() function is available in the json module, and it is used to convert the string values (true/false) to boolean values (True/False).
- json.loads() can convert – “true” to boolean True.
- json.loads() can convert – “false” to boolean False.
Syntax:
json.loads("true".lower())
Parameter:
It takes a string value as a parameter
Example 1
In this example, we will convert the string “true” to boolean True.
# Import the json module import json # Convert true to boolean - True value = json.loads("true".lower()) print(value)
Output:
True
The string is converted to boolean – True.
Example 2
In this example, we will convert the string “false” to boolean False.
import json # Convert false to boolean - False value = json.loads("false".lower()) print(value)
Output:
False
The string is converted to boolean – False.
Convert a string to a boolean using eval()
The eval() function is used to evaluate the expressions. It can be possible to convert the string to a boolean value using this.
- eval(“True”) converts string “True” to True
- eval(“False”) converts string “False” to False
Example 1
In this example, we will convert the string “True” to boolean True.
# Convert True to boolean True print(eval("True"))
Output:
True
The string is converted to boolean – True.
Example 2
In this example, we will convert the string “False” to boolean False.
# Convert False to boolean False print(eval("False"))
Output:
False
The string is converted to boolean – False.
Summary
In this tutorial, we have seen different ways to convert a string to a boolean value – True/False using bool(), json.loads(),eval() and strtobool(). we will directly use the bool() method to convert a string to a boolean value. If the string is empty, then the boolean value is False, Otherwise, it returns True. Whereas, strtobool() available in distutils.util module used to convert the string values to 1 or 0, here 1 represents True and 0 represents False. The json.loads() available in json module is used to convert the string values (true/false) to boolean values (True/False). The eval() method is used to evaluate the expression. It can be used to convert the string to a boolean value. Happy Learning.
Latest Video Tutorials