Skip to main content

philiprehberger-str-case

Tests PyPI version Last updated

Convert strings between camelCase, snake_case, PascalCase, kebab-case, and more.

Installation

pip install philiprehberger-str-case

Usage

from philiprehberger_str_case import to_snake, to_camel, to_pascal, to_kebab

to_snake("HTMLParser")       # "html_parser"
to_camel("html_parser")      # "htmlParser"
to_pascal("get-https-url")   # "GetHttpsUrl"
to_kebab("getHTTPSUrl")      # "get-https-url"

Acronym handling

Acronyms are split correctly:

from philiprehberger_str_case import to_snake

to_snake("HTMLParser")    # "html_parser"
to_snake("getHTTPSUrl")  # "get_https_url"
to_snake("XMLToJSON")    # "xml_to_json"

Number-aware splitting

Numbers at letter-digit boundaries are split into separate words:

from philiprehberger_str_case import to_snake, to_kebab

to_snake("html2json")      # "html_2_json"
to_snake("item123Name")    # "item_123_name"
to_kebab("v2Api")          # "v2-api"  (single-letter prefix stays attached)

More formats

from philiprehberger_str_case import to_constant, to_title, to_dot, to_path

to_constant("htmlParser")  # "HTML_PARSER"
to_title("html_parser")    # "Html Parser"
to_dot("htmlParser")       # "html.parser"
to_path("htmlParser")      # "html/parser"

Sentence case and Header-Case

from philiprehberger_str_case import to_sentence, to_header

to_sentence("html_parser")    # "Html parser"
to_sentence("getHTTPSUrl")    # "Get https url"

to_header("content_type")     # "Content-Type"
to_header("xForwardedFor")    # "X-Forwarded-For"

Split into words

from philiprehberger_str_case import to_words

to_words("getHTTPSUrl")     # ["get", "https", "url"]
to_words("html2json")       # ["html", "2", "json"]
to_words("Content-Type")    # ["content", "type"]

Convert dictionary keys

from philiprehberger_str_case import convert_keys, to_snake

data = {
    "firstName": "John",
    "lastName": "Doe",
    "contactInfo": {
        "emailAddress": "john@example.com",
        "phoneNumber": "555-1234",
    },
}

convert_keys(data, to_snake)
# {
#     "first_name": "John",
#     "last_name": "Doe",
#     "contact_info": {
#         "email_address": "john@example.com",
#         "phone_number": "555-1234",
#     },
# }

Lists of dicts are also supported:

records = [{"firstName": "Alice"}, {"firstName": "Bob"}]
convert_keys(records, to_snake)
# [{"first_name": "Alice"}, {"first_name": "Bob"}]

Detect case

from philiprehberger_str_case import detect_case, is_case

detect_case("my_variable")   # "snake_case"
detect_case("myVariable")    # "camelCase"
detect_case("MyVariable")    # "PascalCase"
detect_case("my-variable")   # "kebab-case"
detect_case("MY_VARIABLE")   # "CONSTANT_CASE"
detect_case("Content-Type")  # "Header-Case"
detect_case("Hello world")   # "Sentence case"

is_case("my_variable", "snake_case")  # True
is_case("my_variable", "camelCase")   # False

API

Function Description
to_snake(s) Convert to snake_case
to_camel(s) Convert to camelCase
to_pascal(s) Convert to PascalCase
to_kebab(s) Convert to kebab-case
to_constant(s) Convert to CONSTANT_CASE
to_title(s) Convert to Title Case
to_dot(s) Convert to dot.case
to_path(s) Convert to path/case
to_sentence(s) Convert to Sentence case
to_header(s) Convert to HTTP Header-Case
to_words(s) Split a string into a list of normalized lowercase words
convert_keys(data, converter) Recursively convert dict/list keys
detect_case(s) Detect case convention of a string
is_case(s, case) Check if string matches a case convention

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

⭐ Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Release files for philiprehberger-str-case 0.3.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 philiprehberger-str-case 0.3.0
File Size Uploaded
philiprehberger_str_case-0.3.0.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-str-case 0.3.0
File Interpreter ABI Platform
philiprehberger_str_case-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.2 kB

Release files / philiprehberger_str_case-0.3.0.tar.gz

Download URL philiprehberger_str_case-0.3.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8ffa5453a81c4fa10ed16e687b95f0f73b4aa71adedcab2614ff1034dee04f90
BLAKE2b-256 checksum
How to use checksums
48e7f3b3fdcddbf9c042ceb4d0b0ef9c07403a23e781372aa5ac07d5d278e80c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / philiprehberger_str_case-0.3.0-py3-none-any.whl

Download URL philiprehberger_str_case-0.3.0-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fc52f180a64f6b19ae89815417101a346590a01f50ab99a7017b1759eb203041
BLAKE2b-256 checksum
How to use checksums
dccfcac001ee71a8df0939028e754d56d93f0dfd4b4ab02e5209c81d6d7bc664
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

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