Hello guys. In this tutorial we will learn about How to convert String to Boolean in Python. THere are so many methods to convert a string into Boolean. So here i am giving you some methods. By using this methods you can convert a string into boolean. Boolean contain two values, First one is true and second is false. So lets learn this how we can convert string to boolean.
How to convert String to Boolean in Python
- Convert String to Boolean in Python
to convert String to Boolean in Python just Use bool(). To Convert String to Boolean in Python you can use bool() function. There are two value of it one is true and the other is false. It you have non empty string than by using bool it will convert your string to true and if the string is empty than it will convert it to false. Here i am using bool() function to convert a string into a boolean in python. So this method will be the easiest for you and it is also very simple. Lets learn this by given below example:
mystr = "How are you" myboolean = bool(mystr) print(myboolean)
Output :True
Another example:mystr = "" myboolean = bool(mystr) print(myboolean)
Output :False
- How to convert String to Boolean in Python
to convert String to Boolean in Python just distutils.util.strtobool(). This function contains two values 1 and0. It is depended on the positive value or negative value. Positive value is like “yes”, “True” and “on”. and the negative value is like “false”, “no” and “off”. The positive value is converted to 1 and the negative value will be converted to 0. You can better understand by example. So lets learn this by given below example.
mystr = distutils.util.strtobool("No") print(mystr)
Output :0
Another example:mystr = distutils.util.strtobool("Yes") print(mystr)
Output :1
Method 1: Use bool()
To Convert String to Boolean in Python you can use bool() function. There are two value of it one is true and the other is false. It you have non empty string than by using bool it will convert your string to true and if the string is empty than it will convert it to false. Here i am using bool() function to convert a string into a boolean in python. So this method will be the easiest for you and it is also very simple. Lets learn this by given below example:
mystr = "How are you"
myboolean = bool(mystr)
print(myboolean)
Output :
True
Another example:
mystr = ""
myboolean = bool(mystr)
print(myboolean)
Output :
False
Method 2: distutils.util.strtobool()
This function contains two values 1 and0. It is depended on the positive value or negative value. Positive value is like “yes”, “True” and “on”. and the negative value is like “false”, “no” and “off”. The positive value is converted to 1 and the negative value will be converted to 0. You can better understand by example. So lets learn this by given below example.
mystr = distutils.util.strtobool("No")
print(mystr)
Output :
0
Another example:
mystr = distutils.util.strtobool("Yes")
print(mystr)
Output :
1
Method 3: Use list comprehension
In this method only one value can be checked either true or false. If the true value will be checked than the other value value automatically falls under the opposite value.Lets learn this by given below example:
mystr = ["False", "True", "False", "False", "True"]
print(str(mystr))
booleanlist = [ele == "False" for ele in mystr]
print(str(booleanlist))
Output :
['False', 'True', 'False', 'False', 'True']
[True, False, True, True, False]
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.