Feature image Convert String to Boolean in Python

Convert String to Boolean in Python: A step-by-step guide

By Brian Laleye · September 13, 2022 · 5 min read

Difficulty Level: Basic

In this article, we will discuss how to convert String to Boolean in Python.

This function is a built-in operation used for handling strings. 

Let’s dive into why this function exists and how to use it in both Code & No Code Python.

Why is the String to Boolean function important?

In Data Analytics, there are many data types (string, number, integer, float, double, date, timestamp…) that data teams have to deal with and select the data types that best suit their problem statement. 

The boolean data type has only two possible values, true or false.

For instance, you want to know if the status of a given user is Active or not in your product but the dataset is as follows:

Your “Activation Status” column is in String type. So let’s dive into how to handle this and convert this data type.

Sample dataset

Convert String to Boolean: The Hard Way by Coding

Method 1: Bool() Function to Convert String to Boolean in Python

The bool() operation takes only one parameter, on which the standard truth testing procedure can be applied.

If no parameter is passed, then False is returned by default.

Example: 

  • Input: 
Input Bool function
				
					CREATE TABLE purchases (
id SERIAL PRIMARY KEY,
orderid VARCHAR NOT NULL,
orderamount VARCHAR NOT NULL,
customerid VARCHAR  NOT NULL,
quantity VARCHAR  NOT NULL,
description VARCHAR NOT NULL
)

				
			
  • Output: 
Output Bool function

Method 2: Eval() Function to Convert String to Boolean in Python

If the string is either True or False, Eval() function can also be used.

The Eval() operation can examine the expression argument and assess it as a Python expression.

Example: 

  • Input
Input Eval function
  • Output: 
Output Eval function

Method 3: Map() and Lambda Functions to Convert String to Boolean in Python

The Map() function is used when a transformation function is applied to each item in an iteration and a new iteration is formed after the transformation.

The Lambda function is an anonymous function in Python. Whenever an anonymous function is declared in a program, we use the keyword lambda.

Example:

  • Input: 
Input Map and Lambda functions
  • Output: 
Output Map and Lambda functions

Convert String to Boolean: The Easy Way with No Code

With this method, you can convert String to Boolean without writing a single line of code.

Step 1: Check the data type of each column of your dataset

  • Input

Hint: Drag-and-drop the Input() function to visualize your dataset and click Play: 

Step 1 Input No Code method
  • Output
Step 1 Output No Code method

Step 2: Search the brackets and Replace them with void

  • Input

Hint: Drag-and-drop the Regex Replace() function, fill in the required fields as follows and click Play:

Step 2 Input No Code method
  • Output
Step 2 Output No Code method

Step 3: Convert your String data type in Boolean

  • Input

Hint: Drag-and-drop the Cast() function and fill in the required fields as follows (select the Boolean type in New Type’s dropdown field) and click Play:

Step 3 Input No Code method
  • Output: 
Step 3 Output No Code method

Conclusion

At RestApp, we’re building a Data Activation Platform for modern data teams.

We designed our next-gen data modeling editor to be intuitive and easy to use.

If you’re interested in starting your data journey, check out our website and create your free account.

Category

Share

Subscribe to our newsletter

Brian Laleye
Brian Laleye
Brian is the co-founder of RestApp. He is a technology evangelist and passionate about innovation. He has an extensive experience focusing on modern data stack.
Share this article
Subscribe to our newsletter
Ready to experience data activation
without code?