Developer Documentation
Python
Updated September 19, 2026

Convert Text Case in Python

Convert words between sentence, title, camel, Pascal, snake, kebab, and constant case in Python.

Case conversion looks simple until punctuation, acronyms, existing camelCase words, Unicode, and line breaks appear. Normalize text into tokens first, then render those tokens into the requested case.

Quick Start#

The following example uses the standard Python API or a widely used package for this task.

Tokenize then render#

Python
1import re23def words(text: str) -> list[str]:4    text = re.sub(r"([a-z0-9])([A-Z])", r"\1 \2", text)5    return [part.lower() for part in re.split(r"[^\w]+", text, flags=re.UNICODE) if part]67def camel_case(text: str) -> str:8    parts = words(text)9    return parts[] + "".join(word.capitalize() for word in parts[:]) if parts else ""1011def snake_case(text: str) -> str:12    return "_".join(words(text))

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Other identifier cases#

Python
1def pascal_case(text: str) -> str:2    return "".join(word.capitalize() for word in words(text))34def kebab_case(text: str) -> str:5    return "-".join(words(text))67def constant_case(text: str) -> str:8    return "_".join(words(text)).upper()

Important Notes#

Python str.title() is convenient but is not a complete editorial title-style implementation. Keep identifier conversion rules explicit.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement Text Case in other languages: