Snake case is a naming convention where words are separated by underscores, usually all in lowercase, such as user_name or total_price.
Quick answer: Write every word in lowercase and put an underscore between them: first_name, order_total. In capitals with underscores, it becomes SCREAMING_SNAKE_CASE, often used for constants.
Key Takeaways
- Underscore separator: Words are joined with
_. - Common in Python: It is the usual style for Python variables and functions.
- Often used in databases: Table and column names such as
customer_idare common. - Has a loud cousin: SCREAMING_SNAKE_CASE uses capitals for constants.
- Style choice, not rule: Teams and languages decide.
Snake Case vs Camel Case
| Feature | snake_case | camelCase |
|---|---|---|
| Example | user_name | userName |
| Separator | Underscore | Capital letter |
| Readability | Clear gaps between words | Compact |
| Typical home | Python, Rust, databases | JavaScript, Java |
freeCodeCamp gives first_name and last_name as examples of snake case. Wikipedia notes that names for other delimiter-separated styles haven’t been standardized. How To Do In Java points out that snake case in database tables and columns, such as invoice_items, is a naming choice and not a requirement shared by every database or project.
Why Snake Case Matters
Underscores create clear visual gaps, which many people find easier to read in long names.
It also avoids the confusion of letter case. In case-insensitive systems, userName and username can clash, while user_name stays distinct.
How to Use Snake Case
Step 1: Lowercase Everything
Convert all letters to lowercase.
Check: No capital letters remain, except in constants.
Step 2: Replace Spaces With Underscores
Swap each space or hyphen for one underscore.
Check: No spaces or hyphens remain.
Step 3: Remove Odd Characters
Strip symbols that aren’t letters, numbers or underscores.
Check: The name is valid in your language.
Step 4: Match Your Project’s Style
Confirm your codebase or database uses the same convention.
Check: New names look like old ones.
An Illustrative Example
A team designs a database with columns customer_id, order_date and total_amount. Their Python code uses the same words as variable names, so the two stay easy to map.
Myths vs Reality
- Myth: Snake case is only for Python. Reality: Other languages and tools use it too.
- Myth: All databases require it. Reality: It is a common choice, not a rule.
- Myth: It means all capitals. Reality: That is SCREAMING_SNAKE_CASE.
CTA: Convert Text to Snake Case
Turn any phrase into snake case, camel case or kebab case in one click. | Open Case Converter | /free-tools/case-converter | teal
Quick Tip
Use SCREAMING_SNAKE_CASE for constants and environment variables, so they stand out from regular variables.
Mistakes to Avoid
- Double underscores by accident: Collapse repeated separators.
- Leading or trailing underscores: They can have special meaning in some languages.
- Mixing with camel case: Stay consistent.
- Using snake case in URLs: Search-friendly URLs usually use hyphens.
- Forgetting numbers: Decide how to handle digits in names.
FAQ
What Is an Example of Snake Case?
first_name and order_total.
What Is SCREAMING_SNAKE_CASE?
Snake case in all capitals, often used for constants.
Which Is Better, Snake or Camel Case?
Neither. Follow the convention of your language or team.
Do URLs Use Snake Case?
Hyphens are usually preferred for URLs. See kebab case.
Is Snake Case Good for Databases?
It is a common choice for table and column names.
How Do I Convert to Snake Case?
Use a case converter or your editor’s text commands.
Where to Go Next
- Capitals: Read about camel case.
- Hyphens: See kebab case.
- URLs: Learn about a slug.
- Data: Compare with JSON keys.
