Convert True and False to 1 and 0 in Python

avatar

Borislav Hadzhiev

Last updated: Aug 28, 2022

banner

Photo from Unsplash

Convert True and False to 1 and 0 in Python #

Use the int() class to convert True and False to 1 and 0, e.g. result = int(True). The int() class will return 1 for True boolean values and 0 for False values.

main.py
# ✅ convert True and False to 1 and 0 bool_t = True int_1 = int(bool_t) print(int_1) # 👉️ 1 bool_f = False int_0 = int(bool_f) print(int_0) # 👉️ 0 # ----------------------------------------------- # ✅ convert 'true' and 'false' to 1 and 0 str_t = 'true' int_1 = int(str_t.lower() == 'true') print(int_1) # 👉️ 1 str_f = 'false' int_0 = int(str_f.lower() == 'true') print(int_0) # 👉️ 0 # ----------------------------------------------- # ✅ convert list of 'true' and 'false' to 1 and 0 my_list = ['true', 'true', 'false'] result = [int(item.lower() == 'true') for item in my_list] print(result) # 👉️ [1, 1, 0]

We used the int() class to convert True to 1 and False to 0.

main.py
result_1 = int(True) print(result_1) # 👉️ 1 result_2 = int(False) print(result_2) # 👉️ 0

The int class returns an integer object constructed from the provided argument.

The constructor returns 0 if no arguments are given.

True boolean values return 1 after conversion to integers and False values return 0.

If you need to convert a list of boolean values to a list of 1 and 0, use a list comprehension.

main.py
my_list = [True, True, False] result = [int(item) for item in my_list] print(result) # 👉️ [1, 1, 0]

We used a list comprehension to iterate over the list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we convert the current boolean value to an integer and return the result.

Convert 'true' and 'false' to 1 and 0 in Python #

To convert 'true' values to 1 and 'false' to 0:

  1. Use the equality operator to check if the value is equal to the string 'true'.
  2. Convert the result of the comparison to an integer.
  3. The conversion will return 1 for 'true' values and 0 for 'false' values.
main.py
str_t = 'true' int_1 = int(str_t.lower() == 'true') print(int_1) # 👉️ 1 str_f = 'false' int_0 = int(str_f.lower() == 'true') print(int_0) # 👉️ 0

We used the lower() method to convert the string to lowercase before comparing it to the string true.

The str.lower method returns a copy of the string with all the cased characters converted to lowercase.

The method doesn't change the original string, it returns a new string. Strings are immutable in Python.

The equality comparison will return True if the string stores a 'true' value and False otherwise.

Converting True to an integer returns 1 and converting False to an integer returns 0.

If you need to convert a list of 'true' and 'false' strings to a list of 1 and 0, use a list comprehension.

main.py
my_list = ['true', 'true', 'false'] result = [int(item.lower() == 'true') for item in my_list] print(result) # 👉️ [1, 1, 0]

On each iteration, we compare the current string to the string 'true' and convert the result of the comparison to an integer.

Strings storing a 'true' value get converted to 1 and all other strings get converted to 0.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.