Convert String to Boolean in Python

Convert String to Boolean in Python

Python has Boolean as one of the in-built data types, and it can return only two possible values true and false. This is what makes this data type ideal and suitable for use in problem statements. It is an essential data type and is frequently used in Python codes.

This article demonstrates different methods to convert string to Boolean in Python.

Using the bool() method to convert string to Boolean in Python.

The bool() is utilized to simply convert a value into a Boolean value. It makes the uses of the standard procedure of truth testing, that is, if the condition passed as a parameter turns out to be true, then it returns the True value, otherwise if the condition passed as parameter is false or no parameter is passed, then it returns the False value.

The following code uses the bool() function to convert String to Boolean in Python.

Output:

True

This function gives a True value when the argument is non-empty, while it always returns a False value for empty arguments.

Output:

False

As there are no arguments passed as parameters in the bool() function, it returns a False value.

Using the distutils.util.strtobool() method to convert a string to Boolean in Python.

The distutils.util module contains a cluster of miscellaneous utility functions that do not fit in any other modules provided by Python. This module needs to be imported to the Python code in order to use the distutils.util.strtobool() method.

The distutils.util.strtobool() method is utilized to convert a string expression to True, which is represented by the value 1, or False, which is represented by 0.

The true and positive values like y, yes, t, and true are converted to 1, while values like n, no, f, false, off are converted to 0. Any other value provided in the variable raises an error of the type ValueError.

The following code uses the distutils.util.strtobool() method to convert a string to Boolean in Python.

Output:

1

Using list comprehension to convert a string to Boolean in Python.

List comprehension is a relatively shorter and very efficient way to create lists that are to be created on the basis of given elements of an already existing list.

This method only checks any out of either the True value or False value, while the other value is automatically considered to be the opposite of the checked value.

The following code uses list comprehension to convert a string to Boolean in Python.

Output:

[‘True’,’False’,’False’,’True’] [False, True, True, False]

In this code, the False values are checked, while the True values are deemed to be True.

Using the map() function along with Lambda function to convert a string to Boolean in Python.

The map() function which is provided by Python is utilized to seek a particular function to all the elements in any given iterable. It returns the iterator itself as the result.

In simple words, a lambda function is a compact anonymous function, which can take any number of arguments, while consisting of only a single expression.

We will use the lambda function along with map() function to achieve the tasking of conversion to Boolean value in Python.

Output:

[‘True’,’False’,’False’,’True’] [False, True, True, False]

The basic concept behind this approach and the list comprehension method is the same, with the only difference being that we have used the map() function along with the lambda function to create a list instead of using list comprehension.

Using the JSON parser to convert a string to Boolean in Python.

JSON is mainly utilized for storage and exchange of data, but can also be used for converting string to built-in Python data types. The json.loads() function can be used to parse a JSON string to the Python code.

To use the json.loads() function, we need to import the json module to the Python code.

The following code uses the JSON parser to convert a string to Boolean in Python.

Output:

False

We should note that this method can only be implemented using the lower() function. It does not work with the upper case upper() function.

Using the eval() function to convert a string to Boolean in Python.

The eval() function can be utilized in parsing the argument passed as the parameter and also in evaluating it as a Python expression.

In this case, the eval() function can be used if the string has either "True" or "False" as its value. If the string matches these conditions and is a legal string, then it will get executed.

The following code uses the eval() function to convert a string to Boolean in Python.

Output:

True

That’s all about convert string to Boolean in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *