Converting from a string to boolean in Python


How do I convert a string into a boolean in Python? This attempt returns True:

>>> bool("False")
Asked By: Joan Venge



Really, you just compare the string to whatever you expect to accept as representing true, so you can do this:

s == 'True'

Or to checks against a whole bunch of values:

s.lower() in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']

Be cautious when using the following:

>>> bool("foo")
>>> bool("")

Empty strings evaluate to False, but everything else evaluates to True. So this should not be used for any kind of parsing purposes.

Answered By: Keith Gaughan

you could always do something like

my_string = "false"
val = (my_string == "true")

the bit in parens would evaluate to False. This is just another way to do it without having to do an actual function call.

Answered By: helloandre
def str2bool(v):
  return v.lower() in ("yes", "true", "t", "1")

Then call it like so:

>>> str2bool("yes")
>>> str2bool("no")
>>> str2bool("stuff")
>>> str2bool("1")
>>> str2bool("0")

Handling true and false explicitly:

You could also make your function explicitly check against a True list of words and a False list of words. Then if it is in neither list, you could throw an exception.

Answered By: Brian R. Bondy

Since Python 2.6 you can use ast.literal_eval, and it’s still available in Python 3.

Evaluate an expression node or a string containing only a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, bytes, numbers, tuples, lists, dicts, sets, booleans, None and Ellipsis.

This can be used for evaluating strings containing Python values without the need to parse the values oneself. It is not capable of evaluating arbitrarily complex expressions, for example involving operators or indexing.

This function had been documented as “safe” in the past without defining what that meant. That was misleading. This is specifically designed not to execute Python code, unlike the more general eval(). There is no namespace, no name lookups, or ability to call out. But it is not free from attack: A relatively small input can lead to memory exhaustion or to C stack exhaustion, crashing the process. There is also the possibility for excessive CPU consumption denial of service on some inputs. Calling it on untrusted data is thus not recommended.

Which seems to work, as long as you’re sure your strings are going to be either "True" or "False":

>>> ast.literal_eval("True")
>>> ast.literal_eval("False")
>>> ast.literal_eval("F")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/Python-2.6.1/lib/python2.6/", line 68, in literal_eval
    return _convert(node_or_string)
  File "/opt/Python-2.6.1/lib/python2.6/", line 67, in _convert
    raise ValueError('malformed string')
ValueError: malformed string
>>> ast.literal_eval("'False'")

I wouldn’t normally recommend this, but it is completely built-in and could be the right thing depending on your requirements.

Answered By: Jacob Gabrielson

The usual rule for casting to a bool is that a few special literals (False, 0, 0.0, (), [], {}) are false and then everything else is true, so I recommend the following:

def boolify(val):
    if (isinstance(val, basestring) and bool(val)):
        return not val in ('False', '0', '0.0')
        return bool(val)
Answered By: Carl G

here’s a hairy, built in way to get many of the same answers. Note that although python considers "" to be false and all other strings to be true, TCL has a very different idea about things.

>>> import Tkinter
>>> tk = Tkinter.Tk()
>>> var = Tkinter.BooleanVar(tk)
>>> var.set("false")
>>> var.get()
>>> var.set("1")
>>> var.get()
>>> var.set("[exec 'rm -r /']")
>>> var.get()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/lib-tk/", line 324, in get
    return self._tk.getboolean(self._tk.globalgetvar(self._name))
_tkinter.TclError: 0expected boolean value but got "[exec 'rm -r /']"

A good thing about this is that it is fairly forgiving about the values you can use. It’s lazy about turning strings into values, and it’s hygenic about what it accepts and rejects(notice that if the above statement were given at a tcl prompt, it would erase the users hard disk).

the bad thing is that it requires that Tkinter be available, which is usually, but not universally true, and more significantly, requires that a Tk instance be created, which is comparatively heavy.

What is considered true or false depends on the behavior of the Tcl_GetBoolean, which considers 0, false, no and off to be false and 1, true, yes and on to be true, case insensitive. Any other string, including the empty string, cause an exception.

You probably already have a solution but for others who are looking for a method to convert a value to a boolean value using “standard” false values including None, [], {}, and “” in addition to false, no , and 0.

def toBoolean( val ):
    Get the boolean value of the provided input.

        If the value is a boolean return the value.
        Otherwise check to see if the value is in 
        ["false", "f", "no", "n", "none", "0", "[]", "{}", "" ]
        and returns True if value is not in the list

    if val is True or val is False:
        return val

    falseItems = ["false", "f", "no", "n", "none", "0", "[]", "{}", "" ]

    return not str( val ).strip().lower() in falseItems
Answered By: Chris McMillan
def str2bool(str):
  if isinstance(str, basestring) and str.lower() in ['0','false','no']:
    return False
    return bool(str)

idea: check if you want the string to be evaluated to False; otherwise bool() returns True for any non-empty string.

Answered By: xvga

Here’s something I threw together to evaluate the truthiness of a string:

def as_bool(val):
 if val:
   if not int(val): val=False
  except: pass
   if val.lower()=="false": val=False
  except: pass
 return bool(val)

more-or-less same results as using eval but safer.

Answered By: tylerl

Here’s is my version. It checks against both positive and negative values lists, raising an exception for unknown values. And it does not receive a string, but any type should do.

def to_bool(value):
       Converts 'something' to boolean. Raises exception for invalid formats
           Possible True  values: 1, True, "1", "TRue", "yes", "y", "t"
           Possible False values: 0, False, None, [], {}, "", "0", "faLse", "no", "n", "f", 0.0, ...
    if str(value).lower() in ("yes", "y", "true",  "t", "1"): return True
    if str(value).lower() in ("no",  "n", "false", "f", "0", "0.0", "", "none", "[]", "{}"): return False
    raise Exception('Invalid value for boolean conversion: ' + str(value))

Sample runs:

>>> to_bool(True)
>>> to_bool("tRUe")
>>> to_bool("1")
>>> to_bool(1)
>>> to_bool(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 9, in to_bool
Exception: Invalid value for boolean conversion: 2
>>> to_bool([])
>>> to_bool({})
>>> to_bool(None)
>>> to_bool("Wasssaaaaa")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 9, in to_bool
Exception: Invalid value for boolean conversion: Wasssaaaaa
Answered By: Petrucio

A dict (really, a defaultdict) gives you a pretty easy way to do this trick:

from collections import defaultdict
bool_mapping = defaultdict(bool) # Will give you False for non-found values
for val in ['True', 'yes', ...]:
    bool_mapping[val] = True

print(bool_mapping['True']) # True
print(bool_mapping['kitten']) # False

If you only want to map known values and throw an exception otherwise:

truthy_strings = ['True', 'yes']  # ... and so on
falsy_strings = ['False', 'no']   # ... and so on

bool_mapping = {}
for v in truthy_strings:
    bool_mapping[v] = True
for v in falsy_strings:
    bool_mapping[v] = False

If you want to be too clever by half, you can shorten this with itertools

from itertools import chain, repeat

bool_mapping = dict(chain(zip(truthy_strings, repeat(True)), zip(falsy_strings, repeat(False))))

This is probably dumb but itertools tricks are vaguely amusing.

Answered By: Nate

This is the version I wrote. Combines several of the other solutions into one.

def to_bool(value):
    Converts 'something' to boolean. Raises exception if it gets a string it doesn't handle.
    Case is ignored for strings. These string values are handled:
      True: 'True', "1", "TRue", "yes", "y", "t"
      False: "", "0", "faLse", "no", "n", "f"
    Non-string values are passed to bool.
    if type(value) == type(''):
        if value.lower() in ("yes", "y", "true",  "t", "1"):
            return True
        if value.lower() in ("no",  "n", "false", "f", "0", ""):
            return False
        raise Exception('Invalid value for boolean conversion: ' + value)
    return bool(value)

If it gets a string it expects specific values, otherwise raises an Exception. If it doesn’t get a string, just lets the bool constructor figure it out. Tested these cases:

test_cases = [
    ('true', True),
    ('t', True),
    ('yes', True),
    ('y', True),
    ('1', True),
    ('false', False),
    ('f', False),
    ('no', False),
    ('n', False),
    ('0', False),
    ('', False),
    (1, True),
    (0, False),
    (1.0, True),
    (0.0, False),
    ([], False),
    ({}, False),
    ((), False),
    ([1], True),
    ({1:2}, True),
    ((1,), True),
    (None, False),
    (object(), True),
Answered By: Tom Ekberg

I like to use the ternary operator for this, since it’s a bit more succinct for something that feels like it shouldn’t be more than 1 line.

True if my_string=="True" else False
Answered By: Clayton Rabenda

I don’t agree with any solution here, as they are too permissive. This is not normally what you want when parsing a string.

So here the solution I’m using:

def to_bool(bool_str):
    """Parse the string and return the boolean value encoded or raise an exception"""
    if isinstance(bool_str, basestring) and bool_str: 
        if bool_str.lower() in ['true', 't', '1']: return True
        elif bool_str.lower() in ['false', 'f', '0']: return False

    #if here we couldn't parse it
    raise ValueError("%s is no recognized as a boolean value" % bool_str)

And the results:

>>> [to_bool(v) for v in ['true','t','1','F','FALSE','0']]
[True, True, True, False, False, False]
>>> to_bool("")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 8, in to_bool
ValueError: '' is no recognized as a boolean value

Just to be clear because it looks as if my answer offended somebody somehow:

The point is that you don’t want to test for only one value and assume the other. I don’t think you always want to map Absolutely everything to the non parsed value. That produces error prone code.

So, if you know what you want code it in.

Answered By: estani

The JSON parser is also useful for in general converting strings to reasonable python types.

>>> import json
>>> json.loads("false".lower())
>>> json.loads("True".lower())
Answered By: Alan Marchiori

A cool, simple trick (based on what @Alan Marchiori posted), but using yaml:

import yaml

parsed = yaml.load("true")
print bool(parsed)

If this is too wide, it can be refined by testing the type result. If the yaml-returned type is a str, then it can’t be cast to any other type (that I can think of anyway), so you could handle that separately, or just let it be true.

I won’t make any guesses at speed, but since I am working with yaml data under Qt gui anyway, this has a nice symmetry.

Answered By: Rafe

I realize this is an old post, but some of the solutions require quite a bit of code, here’s what I ended up using:

def str2bool(value):
    return {"True": True, "true": True}.get(value, False)
Answered By: Ron E

Warning: This answer will no longer work as of Python 3.12 (it’s deprecated as of 3.10)



True values are y, yes, t, true, on and 1; false values are n, no, f, false, off and 0. Raises ValueError if val is anything else.

Be aware that distutils.util.strtobool() returns integer representations and thus it needs to be wrapped with bool() to get Boolean values.

Given that distutils will no longer be part of the standard library, here is the code for distutils.util.strtobool() (see the source code for 3.11.2).

def strtobool (val):
    """Convert a string representation of truth to true (1) or false (0).
    True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
    are 'n', 'no', 'f', 'false', 'off', and '0'.  Raises ValueError if
    'val' is anything else.
    val = val.lower()
    if val in ('y', 'yes', 't', 'true', 'on', '1'):
        return 1
    elif val in ('n', 'no', 'f', 'false', 'off', '0'):
        return 0
        raise ValueError("invalid truth value %r" % (val,))
Answered By: jzwiener

This version keeps the semantics of constructors like int(value) and provides an easy way to define acceptable string values.

def to_bool(value):
    valid = {'true': True, 't': True, '1': True,
             'false': False, 'f': False, '0': False,

    if isinstance(value, bool):
        return value

    if not isinstance(value, basestring):
        raise ValueError('invalid literal for boolean. Not a string.')

    lower_value = value.lower()
    if lower_value in valid:
        return valid[lower_value]
        raise ValueError('invalid literal for boolean: "%s"' % value)

# Test cases
assert to_bool('true'), '"true" is True' 
assert to_bool('True'), '"True" is True' 
assert to_bool('TRue'), '"TRue" is True' 
assert to_bool('TRUE'), '"TRUE" is True' 
assert to_bool('T'), '"T" is True' 
assert to_bool('t'), '"t" is True' 
assert to_bool('1'), '"1" is True' 
assert to_bool(True), 'True is True' 
assert to_bool(u'true'), 'unicode "true" is True'

assert to_bool('false') is False, '"false" is False' 
assert to_bool('False') is False, '"False" is False' 
assert to_bool('FAlse') is False, '"FAlse" is False' 
assert to_bool('FALSE') is False, '"FALSE" is False' 
assert to_bool('F') is False, '"F" is False' 
assert to_bool('f') is False, '"f" is False' 
assert to_bool('0') is False, '"0" is False' 
assert to_bool(False) is False, 'False is False'
assert to_bool(u'false') is False, 'unicode "false" is False'

# Expect ValueError to be raised for invalid parameter...
except ValueError, e:
Answered By: Michael Richmond

If you know that your input will be either "True" or something else, then why not use:

def bool_convert(s):
    return s == "True"
Answered By: Daniel van Flymen

I just had to do this… so maybe late to the party – but someone may find it useful

def str_to_bool(input, default):
    | Default | not_default_str | input   | result
    | T       |  "false"        | "true"  |  T
    | T       |  "false"        | "false" |  F
    | F       |  "true"         | "true"  |  T
    | F       |  "true"         | "false" |  F

    if default:
        not_default_str = "false"
        not_default_str = "true"

    if input.lower() == not_default_str:
        return not default
        return default
Answered By: Rcynic

By using Python’s built-in eval() function and the .capitalize() method, you can convert any “true” / “false” string (regardless of initial capitalization) to a true Python boolean.

For example:

true_false = "trUE"

# OUTPUT: <type 'str'>

true_false = eval(true_false.capitalize())

# OUTPUT: <type 'bool'>
Answered By: elPastor

If you have control over the entity that’s returning true/false, one option is to have it return 1/0 instead of true/false, then:

boolean_response = bool(int(response))

The extra cast to int handles responses from a network, which are always string.

Update 2021: "which are always string" — this is a naive observation. It depends on the serialization protocol used by the library. Default serialization of high-level libraries (the ones used by most web devs) is typically to convert to string before being serialized to bytes. And then on the other side, it’s deserialized from bytes to string, so you’ve lost any type information.

Answered By: Sam Malayek

.blink {
  color: yellow;
  animation: blink-animation 0.5s infinite;
@keyframes blink-animation {
  25% {
    background-color: black;
    color: cyan;
  50% {
    background-color: black;
    color: red;
  75% {
    background-color: black;
    color: lime;

<h1 class="blink">WARNING: Do not use the following code unless you actually know what you are doing with it. Please read the attached disclaimers and make sure you trust your inputs as using this on untrusted inputs could destroy your data and/or cost you your job.</h1>

If you know the string will be either "True" or "False", you could just use eval(s).

>>> eval("True")
>>> eval("False")

Only use this if you are sure of the contents of the string though, as it will throw an exception if the string does not contain valid Python, and will also execute code contained in the string.

Answered By: Joel Croteau

Yet another option

from ansible.module_utils.parsing.convert_bool import boolean
# False
# True
# True
Answered By: Matt Kucia

Use package str2bool pip install str2bool

Answered By: Headmaster

If you like me just need boolean from variable which is string. You can use distils as mentioned by @jzwiener. However I could not import and use the module as he suggested.

Instead I end up using it this way on python3.7

distutils string to bool in python

from distutils import util # to handle str to bool conversion
enable_deletion = 'False'
enable_deletion = bool(util.strtobool(enable_deletion))

distutils is part of the python std lib, so no need to install anything, which is great!

Answered By: Gunay Anach

I use

# function
def to_bool(x):
    return x in ("True", "true", True)

# test cases
[[x, to_bool(x)] for x in [True, "True", "true", False, "False", "false", None, 1, 0, -1, 123]]
[[True, True],
 ['True', True],
 ['true', True],
 [False, False],
 ['False', False],
 ['false', False],
 [None, False],
 [1, True],
 [0, False],
 [-1, False],
 [123, False]]
Answered By: MrHIDEn

NOTE: DON’T EVER USE eval() if it takes an input directly or indirectly from the user because it is highly subject to abuse:

eval('os.system(‘rm -rf /’)')

But cheers! Study finds also that eval() is not evil and it is perfectly OK for TRUSTED CODE. You can use it to convert a boolean string such as "False" and "True" to a boolean type.

I would like to share my simple solution: use the eval(). It will convert the string True and False to proper boolean type IF the string is exactly in title format True or False always first letter capital or else the function will raise an error.


>>> eval('False')

>>> eval('True')

Of course for dynamic variable you can simple use the .title() to format the boolean string.

>>> x = 'true'
>>> eval(x.title())

This will throw an error.

>>> eval('true')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'true' is not defined

>>> eval('false')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'false' is not defined
Answered By: Shift 'n Tab

I was also required to change the input to bool for a function and the main input was only True or False in string. So, I just coded it like this:

def string_to_bool(s):
    bool_flag = True
    if s == "False":
        bool_flag = False
    elif s == "True":
        bool_flag = True
        print("Invalid Input")
    return bool_flag

You can also check it for more shortened for True and False like Y/N or y/n etc.

Answered By: ARHAM RUMI

You can also evaluate any string literal :

import ast
ast.literal_eval('True')  # True
type(ast.literal_eval('True'))  # <class 'bool'>

ls = '[1, 2, 3]'
ast.literal_eval(ls)  # [1, 2, 3]
type(ast.literal_eval(ls))  # <class 'list'>
Answered By: Nomi

I completely agree with the solution of @Jacob Gabrielson but the thing is ast.literal_eval only work with string value of True and False not with true or false. So you just have to use .title() for it to work

import ast
# or
Answered By: Anand Tripathi

By using below simple logic you can convert a string say a = 'true' or 'false', to boolean.

a = a.lower() == 'true'

if a == 'true' then this will set a=True and if a == 'false' then a=False.

Answered By: Sagar Deshmukh

There is an elegant solution with pydantic:

import pydantic

>>> pydantic.parse_obj_as(bool, "true")

>>> pydantic.parse_obj_as(bool, "off")
Answered By: Yann

Use this solution:

def to_bool(value) -> bool:
    if value == 'true':
        return True
    elif value == 'True':
        return True
    elif value == 'false':
        return False
    elif value == 'False':
        return False
    elif value == 0:
        return False
    elif value == 1:
        return True
        raise ValueError("Value was not recognized as a valid Boolean.")
Answered By: srburton

we may need to catch ‘true’ case insensitive, if so:

>>> x="TrUE"  
>>> x.title() == 'True'  

>>> x="false"  
>>> x.title() == 'True'  

also note, it will return False for any other input which is neither true or false

Answered By: RE_Specto

In python version 3.10 you could do something like this;

def stringToBool(string: str) -> bool:
        case 'true':
            return True
        case 'false':
            return False

The match-statement is equivalent to switch in C++.

The top-rated answer is fine for limited cases or situations where you can make strong assumptions about the data you are processing. However, because custom objects can override __eq__ equality checking in Python, there is a significant pitfall. Consider the deliberately over-simplified toy example below:

In [1]: class MyString: 
   ...:     def __init__(self, value): 
   ...:         self.value = value 
   ...:     def __eq__ (self, obj): 
   ...:         if hasattr(obj, 'value'): 
   ...:             return obj.value == self.value 
   ...:         return False 

In [2]: v = MyString("True")                                                                                                                      

In [3]: v == "True"                                                                                                                               
Out[3]: False

If you imagine someone inheriting from a string type for MyString or implementing all kinds of native string methods, repr, etc., so that MyString instances mostly behave exactly like strings, but have the special extra value step in equality checking, then simple use of == 'True' would fail, and most likely it would be a silent failure from the user’s perspective.

This is why it’s good practice to coerce type into the exact nature of equality checking you want to perform, put that encapsulated into a helper function, and be pedantic about relying on that kind of "registered" way to validate things. For example with MyString you might write something like this,

def validate(s):
    if isinstance(s, str):
        return s == 'True'
    elif isinstance(s, MyString):
        return s.value == 'True' # <-- business logic
    raise ValueError(f"Type {type(s)} not supported for validation.")

Or another often used pattern is the reverse perspective where you define exactly one behavior for validation but you have a helper function that forces coercion into a type amenable for that single validation behavior, such as

def to_str(s):
    if isinstance(s, str):
        return s
    elif isinstance(s, MyString):
        return s.value
    raise ValueError(f"Unsupported type {type(s)}")

def validate(s):
    return to_str(s) == 'True'

It might look like we’re adding a lot of boilerplate and verbosity. We could glibly express critique by saying, "why write all that if you can just write s == 'True'?" – But it misses the point that when you are validating something, you need to make sure all of your preconditions hold for the validation logic to be applied. If you can assume some data is a plain str type and you don’t need to do any of that precondition (such as type) checking, great – but that’s a very rare situation and it can be misleading to characterize the general situation for this question as being amenable to one super short and concise equality check.

Answered By: ely

This is an answer that uses code from the Django Rest Framework (DRF) 3.14.

You can either:

from rest_framework.fields import BooleanField
f = BooleanField(allow_null=True)
test_values = [ True, "True", "1", 1, -1, 1.0, "true", "t", "on",
         None, "null", "NULL",
         False, "False", "0", 0, "false", "f", 0.0, "off" ]
for item in test_values:
    r = f.to_internal_value(item)
# a shorter version
from rest_framework.fields import BooleanField
test_values = [ True, "True", "1", 1, -1, 1.0, "true", "t", "on",
         None, "null", "NULL",
         False, "False", "0", 0, "false", "f", 0.0, "off" ]
for item in test_values:

Or you could adapt the code of the BooleanField so that it suits your need. Here is the actual code of the class BooleanField as in DRF 3.x

# from rest_framework.fields 
# ...

class BooleanField(Field):
    default_error_messages = {
        'invalid': _('Must be a valid boolean.')
    default_empty_html = False
    initial = False
        't', 'T',
        'y', 'Y', 'yes', 'Yes', 'YES',
        'true', 'True', 'TRUE',
        'on', 'On', 'ON',
        '1', 1,
        'f', 'F',
        'n', 'N', 'no', 'No', 'NO',
        'false', 'False', 'FALSE',
        'off', 'Off', 'OFF',
        '0', 0, 0.0,
    NULL_VALUES = {'null', 'Null', 'NULL', '', None}
    def to_internal_value(self, data):
            if data in self.TRUE_VALUES:
                return True
            elif data in self.FALSE_VALUES:
                return False
            elif data in self.NULL_VALUES and self.allow_null:
                return None
        except TypeError:  # Input is an unhashable type
            pass'invalid', input=data)
    def to_representation(self, value):
        if value in self.TRUE_VALUES:
            return True
        elif value in self.FALSE_VALUES:
            return False
        if value in self.NULL_VALUES and self.allow_null:
            return None
        return bool(value)

# ...
Answered By: Ouss
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.