Python Case Conversion
Overview
This skill provides comprehensive guidance on converting strings between different case formats in Python using the pydash library. Convert between camelCase, snake_case, kebab-case, PascalCase, and many other formats commonly used in programming.
When to Use This Skill
Use this skill when:
- •Converting variable names between different naming conventions (e.g., Python's snake_case to JavaScript's camelCase)
- •Transforming API response field names to match local code conventions
- •Creating URL-friendly slugs from titles or descriptions
- •Formatting strings for display purposes (title case, start case, human case)
- •Adapting code between different programming languages with different naming conventions
- •Bulk renaming variables or fields to follow a style guide
- •Working with database column names and converting them to/from code variable names
Quick Start
To convert between string cases in Python, use the pydash library:
import pydash as _
# Convert to camelCase
_.camel_case("foo bar baz") # "fooBarBaz"
# Convert to snake_case
_.snake_case("FooBarBaz") # "foo_bar_baz"
# Convert to kebab-case
_.kebab_case("Foo Bar Baz") # "foo-bar-baz"
# Convert to PascalCase
_.pascal_case("foo bar baz") # "FooBarBaz"
# Create URL slug
_.slugify("Hello World!") # "hello-world"
Common Conversion Tasks
Converting Variable Names Between Languages
When working with APIs or multi-language codebases:
import pydash as _ # Python to JavaScript python_var = "user_email_address" js_var = _.camel_case(python_var) # "userEmailAddress" # JavaScript to Python js_field = "firstName" python_field = _.snake_case(js_field) # "first_name" # Python to CSS python_class = "primary_button" css_class = _.kebab_case(python_class) # "primary-button"
Creating URL Slugs
For blog posts, articles, or any URL-friendly strings:
import pydash as _ title = "My Amazing Blog Post!" slug = _.slugify(title) # "my-amazing-blog-post" # With custom separator slug = _.slugify(title, separator="_") # "my_amazing_blog_post"
Display-Friendly Labels
Convert code variable names to user-friendly labels:
import pydash as _
# Database column to label
_.start_case("created_at") # "Created At"
_.title_case("user_email") # "User Email"
# Special handling for _id suffix
_.human_case("user_id") # "User" (removes _id)
Bulk Transformations
When converting multiple fields at once:
import pydash as _
# Convert API response keys
api_response = {
"firstName": "John",
"lastName": "Doe",
"emailAddress": "john@example.com"
}
python_response = {
_.snake_case(key): value
for key, value in api_response.items()
}
# Result: {"first_name": "John", "last_name": "Doe", "email_address": "john@example.com"}
Available Case Conversion Functions
For a complete reference of all available functions, see references/case_conversion_functions.md.
Key functions include:
- •
camel_case()- Convert to camelCase (first letter lowercase) - •
pascal_case()- Convert to PascalCase (first letter uppercase) - •
snake_case()- Convert to snake_case (underscores) - •
kebab_case()- Convert to kebab-case (hyphens) - •
slugify()- Convert to URL-safe slug - •
title_case()- Convert to Title Case - •
start_case()- Convert to Start Case - •
human_case()- Convert to human-friendly format - •
upper_case()- Convert to UPPER CASE (with spaces) - •
lower_case()- Convert to lower case (with spaces) - •
separator_case(separator)- Convert using custom separator
Programming Convention Reference
Different contexts require different naming conventions:
| Context | Convention | Function |
|---|---|---|
| Python variables/functions | snake_case | snake_case() |
| Python classes | PascalCase | pascal_case() |
| JavaScript variables/functions | camelCase | camel_case() |
| JavaScript classes | PascalCase | pascal_case() |
| CSS classes | kebab-case | kebab_case() |
| URLs | lowercase-with-hyphens | slugify() |
| Database columns | snake_case | snake_case() |
| Constants | SCREAMING_SNAKE_CASE | snake_case() + to_upper() |
| Display text | Title Case | title_case() or start_case() |
Installation
If pydash is not installed, install it with:
pip install pydash
Detailed Reference
For comprehensive documentation including:
- •Complete function reference table with examples
- •Detailed usage patterns for each function
- •Unicode and special character handling
- •Advanced use cases and edge cases
See: references/case_conversion_functions.md