Convert Boolean to Int Python | Example code

Use int for casting a Boolean value in Python. Using int() method will convert Boolean to Int, 1 for True and 0 for False.

Example convert boolean to int python

Simple python example code. Do not use a lowercase boolean value otherwise, it will throw an error. (invalid literal for int() with base 10: ‘true’)

answer = True

answer = False


Convert Boolean to Int Python

Another way is using if else condition

bool_val = True
print("Initial value", bool_val)

# Converting boolean to integer
if bool_val:
    bool_val = 1
    bool_val = 0

print("Converted Int value", bool_val)


Initial value True
Converted Int value 1

Do comment if you have any doubts and another example on this Python Boolean int topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.