Converting from a string

Basic types / Boolean

# Compare the text in lower case
str_value = "True"
bool_value1 = str_value.lower() == "true"
# bool_value1 is True

# Several words count as true
str_answer = "yes"
bool_value2 = str_answer.lower() in ("true", "yes", "1")
# bool_value2 is True

# Trap: bool() is not a parser, "false" is a non-empty string
bool_value3 = bool("false")
# bool_value3 is True

# The empty string is the only false one
bool_value4 = bool("")
# bool_value4 is False

print(f"{bool_value1 = }")
print(f"{bool_value2 = }")
print(f"{bool_value3 = }")
print(f"{bool_value4 = }")