Registry / serialization / str2bool

str2bool

JSON →
library1.1pypypi✓ verified 84d ago

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.

pip install str2bool
INSTALL
IMPORT
SIG · STR2BOOL
S
str2bool
serializationpythonv1.1
Install
2.4s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.2MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.4s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

str2bool
from str2bool import str2bool

Demonstrates importing the `str2bool` function and converting various string inputs. Unrecognized strings default to `False`.

from str2bool import str2bool # Example usage print(f"'Yes' -> {str2bool('Yes')}") print(f"'no' -> {str2bool('no')}") print(f"'1' -> {str2bool('1')}") print(f"'FALSE' -> {str2bool('FALSE')}") print(f"'invalid_string' -> {str2bool('invalid_string')}") # Returns False by default for unrecognized strings
Debug
Known issues
gotchaThis 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.
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.
affects: <=1.1
gotchaThe 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.
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.
affects: All
deprecatedThe `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.
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.
affects: Python 3.10+ (for `distutils.util.strtobool`)
Errors
Common errors & fixes
ValueError: Invalid value 'some_invalid_string'. Expected one of: false, f, n, no, t, true, y, yes, 0, 1.
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.
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`.
ModuleNotFoundError: No module named 'str2bool'
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.
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`.
Upgrade
Version history
1.1latest on PyPI · released Dec 25, 2017
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Resources
str2bool — pip install str2bool · libregistry