{"id":9339,"library":"str2bool","title":"str2bool","description":"This library provides a simple function to convert string representations to boolean values. It explicitly recognizes 'yes', 'true', 'y', 't', '1' as True, and 'no', 'false', 'n', 'f', '0' as False, with case-insensitivity. This version (1.1) was released in 2017 and is currently in maintenance mode with no active development.","status":"maintenance","version":"1.1","language":"en","source_language":"en","source_url":"https://github.com/symonsoft/str2bool","tags":["string","boolean","type conversion","utility"],"install":[{"cmd":"pip install str2bool","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"str2bool","correct":"from str2bool import str2bool"}],"quickstart":{"code":"from str2bool import str2bool\n\n# Example usage\nprint(f\"'Yes' -> {str2bool('Yes')}\")\nprint(f\"'no' -> {str2bool('no')}\")\nprint(f\"'1' -> {str2bool('1')}\")\nprint(f\"'FALSE' -> {str2bool('FALSE')}\")\nprint(f\"'invalid_string' -> {str2bool('invalid_string')}\") # Returns False by default for unrecognized strings","lang":"python","description":"Demonstrates importing the `str2bool` function and converting various string inputs. Unrecognized strings default to `False`."},"warnings":[{"fix":"Consider using `pip install str2bool3` and `from str2bool3 import StrUtils` (then `StrUtils.str2bool(...)`) for an actively maintained fork, or `pip install python-strtobool` and `from str_to_bool import str_to_bool` for an alternative.","message":"This library is not actively maintained since 2017. Newer, actively maintained alternatives like `str2bool3` (a fork of this project) or `python-strtobool` offer similar or enhanced functionality and better Python version compatibility.","severity":"gotcha","affected_versions":"<=1.1"},{"fix":"Understand that `str2bool` is designed to interpret string content (e.g., 'True', 'False') while `bool()` interprets the truthiness of the string object itself (e.g., an empty string is `False`, any other string is `True`). Use `str2bool` when you need to parse a string's explicit boolean meaning.","message":"The built-in `bool()` constructor behaves differently than `str2bool`. `bool('False')` evaluates to `True` because non-empty strings are considered truthy in Python's native boolean conversion.","severity":"gotcha","affected_versions":"All"},{"fix":"For new projects, avoid `distutils.util.strtobool`. Implement a custom string-to-boolean parsing function, or use a dedicated library like `str2bool` or its modern alternatives.","message":"The `distutils.util.strtobool` function, a common alternative for string-to-boolean conversion often suggested in older Python documentation, is deprecated in Python 3.10 and removed in Python 3.12. While this `str2bool` library does not use it, users might encounter issues if they switch to it from `distutils.util.strtobool` in newer Python environments.","severity":"deprecated","affected_versions":"Python 3.10+ (for `distutils.util.strtobool`)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"If you are encountering this `ValueError`, you are likely using `str2bool3` (or another fork) and calling `StrUtils.str2bool_exc()`. If you expect non-boolean strings and want them to default to `False`, use `StrUtils.str2bool()` instead or handle the `ValueError` explicitly. If you intended to use the original `str2bool` (v1.1), ensure `pip install str2bool` was run and you are importing `from str2bool import str2bool`.","cause":"The original `str2bool` library (v1.1) returns `False` by default for any unrecognized string input without raising an exception. This error message is from the `str2bool_exc` function of the `str2bool3` fork, which explicitly raises a `ValueError` for invalid inputs.","error":"ValueError: Invalid value 'some_invalid_string'. Expected one of: false, f, n, no, t, true, y, yes, 0, 1."},{"fix":"Ensure the correct package is installed by running `pip install str2bool`. If you intended to use `str2bool3`, install it with `pip install str2bool3` and adjust imports to `from str2bool3 import StrUtils`. If using `python-strtobool`, install `pip install python-strtobool` and import `from str_to_bool import str_to_bool`.","cause":"The `str2bool` package has not been installed in your current Python environment, or you are trying to import `str2bool` but a different string-to-boolean library (e.g., `str2bool3` or `python-strtobool`) was installed instead.","error":"ModuleNotFoundError: No module named 'str2bool'"}]}