Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does `bool("False")` evaluate to `True` in Python?

Quick answer

bool("False") is True because Python treats any nonempty string as true in a Boolean context; it does not interpret the word inside the string. By contrast, bool("") and bool(False) are both False. The quotation marks distinguish the string "False" from the Boolean value False. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

Why does bool("False") evaluate to True in Python? I expected the word “False” to mean the Boolean value False.

Geez

Python checks whether a value is truthy, not whether a string spells a Boolean word. A nonempty string is true in a Boolean context, so "False" produces True; an empty string produces False. The actual Boolean False is false. [S1]

These are different values:

print(bool("False"))  # True: nonempty string
print(bool(""))       # False: empty string
print(bool(False))    # False: Boolean value

The quotation marks matter: "False" is text, while False without quotation marks is a Boolean.

Follow-up question

Can I compare a few values side by side to see the distinction?

Geez

Yes. This runnable example prints each value and its type alongside its truth value:

values = ["False", "", False, "0", 0]

for value in values:
    print(repr(value), type(value).__name__, bool(value))

The key results are "False" → True, "" → False, and False → False. The string "0" is also true because it is nonempty; the number 0 is false. Python treats empty strings and numeric zero as false, while nonempty strings are true. [S1] This comparison shows truthiness, not conversion of text into a Boolean.

Follow-up question

My value comes from text input and might contain the word False. How should I turn that text into a Boolean?

Geez

Check the text’s content explicitly instead of calling bool() on it. For example, if your accepted inputs are only true and false (regardless of capitalization or surrounding spaces), validate those choices and reject anything else:

text = "False"  # Example input; replace with your text value.
normalized = text.strip().lower()

if normalized == "true":
    enabled = True
elif normalized == "false":
    enabled = False
else:
    raise ValueError("Expected true or false")

print(enabled)  # False

That code defines a particular input rule: it accepts those two words after trimming and lowercasing. It does not mean Python’s bool() parses strings. Decide what inputs your program should accept, and handle other values deliberately.

Follow-up question

What should I check if my condition behaves unexpectedly?

Geez

Inspect both the value and its type, then decide whether you want a truthiness check or a text-to-Boolean conversion. For example, print(repr(value), type(value).__name__) can reveal whether a value is the string "False" or the Boolean False. If it is a string, if value: checks whether the string is nonempty; it does not interpret the word inside it. [S1]

A useful rule of thumb: use if value: when you mean “is this value nonempty or otherwise truthy?” Use explicit validation and comparison when you mean “does this text represent true or false?” The truthiness comparison alone cannot establish what input your application should accept; that is a rule you must choose.

A practical checklist

  • Check whether the value is a string at runtime or the Boolean value False.

  • Use repr(value) and type(value).__name__ to inspect the actual value and type.

  • Use bool(value) only when you want Python’s truthiness behavior.

  • For text input, define accepted true and false spellings and validate them explicitly.

  • Decide how unexpected or blank input should be handled.

A few more useful answers

Does `bool("False")` change the string into the Boolean `False`?

No. It returns the truth value of the string, which is True because the string is nonempty. [S1]

Why is `bool("")` false but `bool("0")` true?

The empty string has no contents and is false in a Boolean context; "0" contains a character and is a nonempty string, so it is true. [S1]

How do I convert only the words `true` and `false`?

Normalize the text, compare it with the accepted words, and handle anything else explicitly. The example above accepts either word without regard to capitalization or surrounding spaces.

Sources

References used for this guide.

  1. [S1] Expressions — Boolean operations and truth values
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work