How True is bool(‘False’)

Foong Min Wong
Feb 17, 2023

--

A few days ago, I was debugging something at work and found out that bool(“False”) returns True in Python.

Given you have a dataframe that contains a string column with the values True or False. If you simply cast that string column to boolean type and use it to generate another column, you get unexpected results:

My mistake! bool(“string”) always returns True.

To convert True/False strings to boolean in Python, there are many ways to do it, as stated in this link. One of them is by using eval(), which parses the expression argument and evaluates it as a python expression.

Python eval() function

Code Example:

I went swimming in the pool at the end of work, and now writing something about the bool. Pretty cool.

--

--