kebab-case vs snake_case: When to Use Hyphens or Underscores
Hyphens and underscores look similar but serve different ecosystems. Learn when kebab-case fits URLs and CSS, and when snake_case fits code and databases.
Quick answer
Use kebab-case (hello-world) for URL slugs, CSS class names, and many CLI flags. Use snake_case (hello_world) for Python variables, SQL columns, and Ruby identifiers. The separator is not interchangeable—Python cannot use hyphens in variable names, and URLs rarely use underscores.
kebab-case in URLs and front-end code
Lowercase hyphenated paths read well in browser bars and analytics: /blog/post-title/ is easier to scan than mixed_case or CamelCase routes. CSS utilities and component classes (nav-item, card-title) follow the same pattern. When marketing hands you a Title Case headline, convert it to a slug before publishing.
snake_case in Python, data, and SQL
PEP 8 recommends snake_case for Python functions and variables. Warehouse tables and ORM models often map class names in PascalCase to snake_case columns. JSON from Python APIs may use snake_case keys while JavaScript clients expect camelCase—document the mapping at your API boundary.
How teams avoid drift
Write a one-page naming guide: public URLs kebab-case, database snake_case, JSON camelCase, TypeScript types PascalCase. When you import a spreadsheet of “Order Line Item” labels, run each through the right converter instead of retyping. Code review catches inconsistent slugs faster when generators own the first pass.
Common mistakes
Mixing separators in one project (user_profile-id) confuses search and codegen tools. Using ALL CAPS slugs creates duplicate URL variants on case-sensitive servers—lowercase first, then kebab-case. Copying snake_case column names directly into URL paths without conversion breaks analytics filters that expect hyphens.
Related resources: kebab-case Converter • snake_case Converter • camelCase vs snake_case • Case converters hub
Frequently Asked Questions
Which is better for SEO: hyphens or underscores in URLs?
Search engines treat hyphens as word separators in URLs. Underscores are less common in paths and can look like spaces in some contexts. Lowercase kebab-case is the safer default for public URLs.
Can I use kebab-case in Python?
No. Hyphens are minus operators in Python. Use snake_case for variables and functions.
How do I convert between them?
Use a kebab-case converter for slugs and a snake_case converter for code identifiers. Start from the same plain-English phrase and compare outputs side by side.