Check if user input is a Boolean in Python

avatar

Borislav Hadzhiev

Last updated: Aug 24, 2022

banner

Photo from Unsplash

Check if user input is a Boolean in Python #

To check if user input is a boolean:

  1. Use the input() function to take input from the user.
  2. Check if the provided value is equal to the strings True or False.
  3. Perform an action if either condition is met.
main.py
# ✅ Check if value is a boolean if isinstance(True, bool): # 👇️ this runs print('The value is a boolean') # --------------------------------------- # ✅ Check if user input is a boolean user_input = input('Subscribe to newsletter? True / False: ') if user_input.capitalize() == 'True': print('The user typed in True') elif user_input.capitalize() == 'False': print('The user typed in False') else: print('Enter True or False') # --------------------------------------- # ✅ Keep asking user for input until they enter a boolean value user_input = '' while True: user_input = input('Subscribe to newsletter? True / False: ') if user_input.capitalize() == 'True': print('The user typed in True') break elif user_input.capitalize() == 'False': print('The user typed in False') break else: print('Enter True or False') continue

The first example checks if a value is boolean.

main.py
if isinstance(True, bool): print('The value is a boolean') if isinstance('True', bool): print('The value is boolean') else: # 👇️ this runs print('The value is not boolean')

The isinstance function returns True if the passed in object is an instance or a subclass of the passed in class.

However, the input() function is always guaranteed to return a value of type string, so this check would fail.

Converting any non-empty string to a boolean returns True, so that wouldn't work either.

main.py
print(bool('a')) # 👉️ True print(bool('False')) # 👉️ True print(bool('')) # 👉️ False

This is why we compare the user input value to the strings True and False instead.

main.py
user_input = input('Subscribe to newsletter? True / False: ') if user_input.capitalize() == 'True': print('The user typed in True') elif user_input.capitalize() == 'False': print('The user typed in False') else: print('Enter True or False')

The if statement checks if the user input value is equal to the string True.

The str.capitalize function returns a copy of the string with the first character capitalized and the rest lowercased.

main.py
print('true'.capitalize()) # 👉️ 'True' print('FALSE'.capitalize()) # 👉️ 'False'

The str.capitalize method makes sure the first letter of the input value is uppercase and the rest are lowercase.

check if input is boolean

If you need to ask the user for input until they enter a boolean value, use a while loop.

main.py
user_input = '' while True: user_input = input('Subscribe to newsletter? True / False: ') if user_input.capitalize() == 'True': print('The user typed in True') break elif user_input.capitalize() == 'False': print('The user typed in False') break else: print('Enter True or False') continue

keep asking user for input until boolean response

The if statement checks if the user input value is equal to the string True.

If the condition is met, we print the value and break out of the while loop.

The break statement breaks out of the innermost enclosing for or while loop.

If the provided value is neither True nor False, we use the continue statement to prompt the user again.

The continue statement continues with the next iteration of the loop.

When validating user input in a while loop, we use the continue statement when the input is invalid, e.g. in an except block or an if statement.

If the input is valid, we use the break statement to exit out of the while loop.

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.