Skip to main content

str-or-none

A tiny Python utility to safely convert a value to a str or None.

  • Returns a stripped string if possible, or None if the value is None.
  • Raises ValueError for non-string types in strict mode (default).
  • Supports file-like objects, numbers, datetimes, sequences, dicts, and custom objects.
  • Can be used as a function or directly as a module call.

Usage

import str_or_none
# or: from str_or_none import str_or_none

str_or_none("  hello  ")           # 'hello'
str_or_none(None)                   # None
str_or_none(123)                    # ValueError (strict mode)
str_or_none(123, strict=False)      # '123'
str_or_none([1, 2, 3], strict=False) # '[1, 2, 3]'
str_or_none({"a": 1}, strict=False) # '{"a": 1}'

import io
str_or_none(io.StringIO("  hi  ")) # 'hi'
str_or_none("  hi  ", strip=False)  # '  hi  '

class Custom:
    def __str__(self):
        return "custom!"

str_or_none(Custom(), strict=False)  # 'custom!'
str_or_none("")  # None (default: empty_str_to_none=True)
str_or_none("", empty_str_to_none=False)  # ''
str_or_none("   ")  # None (after strip)
str_or_none("   ", empty_str_to_none=False)  # ''

API

def str_or_none(value, *, strip=True, strict=True, empty_str_to_none=True) -> Optional[str]:
    ...
  • strip: Remove leading/trailing whitespace (default: True)
  • strict: Raise ValueError for non-string types (default: True)
  • empty_str_to_none: If True, return None for empty strings (after optional strip). Default: True.

Returns a string or None, or raises ValueError if conversion is not allowed.

Metadata

Release files for str-or-none 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for str-or-none 0.1.0
File Size Uploaded
str_or_none-0.1.0.tar.gz 2.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for str-or-none 0.1.0
File Interpreter ABI Platform
str_or_none-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 6.1 kB

Release files / str_or_none-0.1.0.tar.gz

Download URL str_or_none-0.1.0.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
ccced5fe5eb8d953fa1f1a708416366afe5a0e2682c3112c1843766a4633e808
BLAKE2b-256 checksum
How to use checksums
6ad1ccd34835b091493ad6a763f45026734d6a144980eae4a482b4b5926d5036
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.8.20 Darwin/24.4.0

Release files / str_or_none-0.1.0-py3-none-any.whl

Download URL str_or_none-0.1.0-py3-none-any.whl
Size 3.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2ee71ce3bffd169af56e5b85ba841f6af71d27bc4c93e49c51cbfe68f608caaf
BLAKE2b-256 checksum
How to use checksums
947511de071666160778f9822f1b921902a3f00773b7f32f38673748957140ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.8.20 Darwin/24.4.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page