Kodeclik Blog
How to convert a string to boolean in Python
Note that a boolean value is either True or False in Python and you might sometimes find a need to convert a string to a boolean. For instance consider the following program:
sickly = input("Are you feeling sick? ")
if (sickly):
print("You should stay home or goto the doctor.")
else:
print("Lets go out and play!")
In this program we are obtaining input from the user to the question “Are you feeling sick?”. This input is stored in variable “sickly”. Then we are checking for the value of sickly and recommending a course of action, either stay home/go see the doctor vs going out and playing. Here is what the program does for different inputs:
Are you feeling sick? Yes
You should stay home or goto the doctor.
Are you feeling sick? No
You should stay home or goto the doctor.
Are you feeling sick?
Lets go out and play!
The first question-answer interaction makes it look like the program is working as intended until we realize that it gives the same response for “No” as it gives for “Yes”. The reason is because any non-empty input evaluates to True so the if clause evaluates to True. On the other hand, in the third interaction, we press Enter without any input and thus the input is empty which evaluates to False.
Obviously the program needs to be modified. In particular, we need a way to convert the Python string to a true boolean. Here is how that can work:
def string_to_bool(s):
if s in ["True", "true", "Yes", "yes"]:
return (True)
else:
return (False)
sickly = input("Are you feeling sick? ")
sickly = string_to_bool(sickly)
if (sickly):
print("You should stay home or goto the doctor.")
else:
print("Lets go out and play!")
As you can see above, we create a function called “string_to_bool” that takes a string as input (s) and returns a boolean. First we check if the string is part of a “whitelist” of values that we consider to evaluate to True. If so we return True (note that this is True, the boolean, not “True” which is a string). Alternatively, we return False.
In the main part of the program, after we receive user input we convert it to a boolean before deciding on the message to print. Here are some sample interactions:
Are you feeling sick? True
You should stay home or goto the doctor.
Are you feeling sick? Yes
You should stay home or goto the doctor.
Are you feeling sick? No
Lets go out and play!
Are you feeling sick? Yes!
Lets go out and play!
Everything works as intended. Note that in the last (fourth) interaction above, “Yes!” (with an exclamation) is not part of the white list and thus the program evaluates it to False. If you wish such variations to be included, you must either expand the whitelist or can see if one of the whitelist words is part of the input. We leave this as an exercise.
In summary, there are many situations like the above where you will need to convert a string to a boolean in Python. The best solution is to create a custom function like we have done here.
If you liked this blogpost, learn how to do the reverse, i.e., how to convert a boolean in Python to a string!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.