Text Case Converter — Developer Code Samples
Convert text between different naming conventions: camelCase for JavaScript, snake_case for Python, PascalCase for classes, and kebab-case for CSS. These utilities are essential for code generators, API bridges, and linting tools.
Try the interactive version online:
Open Text Case Converter Tool →
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| text | str | Yes | Input text in any case format |
| to_case | str | Yes | Target case: snake, camel, pascal, kebab, constant, title (required) |
Returns: String converted to the specified naming convention
Code Examples
import re
def to_snake_case(text):
"""Convert any case to snake_case."""
# Handle camelCase and PascalCase
text = re.sub(r'([A-Z]+)([A-Z][a-z])', r'\1_\2', text)
text = re.sub(r'([a-z\d])([A-Z])', r'\1_\2', text)
# Replace spaces and hyphens
text = re.sub(r'[\s-]+', '_', text)
return text.lower()
def to_camel_case(text):
"""Convert any case to camelCase."""
text = to_snake_case(text)
parts = text.split('_')
return parts[0] + ''.join(p.capitalize() for p in parts[1:])
def to_pascal_case(text):
"""Convert any case to PascalCase."""
text = to_snake_case(text)
return ''.join(p.capitalize() for p in text.split('_'))
def to_kebab_case(text):
"""Convert any case to kebab-case."""
return to_snake_case(text).replace('_', '-')
def to_title_case(text):
"""Convert to Title Case (capitalize each word)."""
return re.sub(r"[A-Za-z]+('[A-Za-z]+)?", lambda m: m.group(0).capitalize(), text)
def to_constant_case(text):
"""Convert to CONSTANT_CASE (uppercase snake)."""
return to_snake_case(text).upper()
# Examples
inputs = ["hello world", "helloWorld", "HelloWorld", "hello-world", "HELLO_WORLD"]
for inp in inputs:
print(f"Input: {inp!r}")
print(f" snake_case: {to_snake_case(inp)}")
print(f" camelCase: {to_camel_case(inp)}")
print(f" PascalCase: {to_pascal_case(inp)}")
print(f" kebab-case: {to_kebab_case(inp)}")
print(f" CONSTANT_CASE: {to_constant_case(inp)}")