Snake Case — What It Is, When to Use It, and How to Convert
Snake case uses underscores between words in lowercase: hello_world, user_first_name. It's the standard naming convention for Python, SQL, and file names. Here's when to use...
Snake case writes multi-word identifiers with underscores between words, all in lowercase: hello_world, user_first_name, max_retry_count. It’s named after how the words lie flat, like a snake.
Use the Case Converter to convert any text to snake_case instantly.
Snake case rules
user name → user_name
getUserProfile → get_user_profile
First Name → first_name
HTTP_REQUEST → http_request (or keep as HTTP_REQUEST for constants)
- All letters lowercase
- Words separated by underscores
- No spaces or hyphens
- Numbers are allowed:
user_id_123
Where snake_case is standard
Python
Python’s PEP 8 style guide mandates snake_case for:
# Variables and function names:
user_name = "Alice"
max_retry_count = 3
def get_user_profile(user_id):
return db.query(user_id)
def calculate_total_price(items, tax_rate):
subtotal = sum(item.price for item in items)
return subtotal * (1 + tax_rate)
# Module names:
# user_service.py, data_processor.py, auth_middleware.py
UPPERCASE with underscores is used for constants:
MAX_RETRIES = 5
DEFAULT_TIMEOUT = 30
API_BASE_URL = "https://api.example.com"
DATABASE_URL = "postgresql://localhost/mydb"
SQL
SQL conventionally uses snake_case for table and column names:
CREATE TABLE user_profiles (
user_id INTEGER PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email_address VARCHAR(100),
created_at TIMESTAMP,
is_active BOOLEAN
);
SELECT user_id, first_name, last_name
FROM user_profiles
WHERE is_active = true
ORDER BY created_at DESC;
File and directory names
Snake_case is common for file names, especially in Python projects:
user_service.py
data_processor.py
test_user_authentication.py
Ruby and PHP
Ruby and PHP also use snake_case for variables and methods:
user_name = "Alice"
def calculate_discount(original_price, discount_rate)
original_price * discount_rate
end
Converting to snake_case in code
JavaScript
function toSnakeCase(str) {
return str
.replace(/([A-Z])/g, '_$1') // Insert _ before capitals
.replace(/[\s-]+/g, '_') // Replace spaces/hyphens with _
.replace(/^_/, '') // Remove leading _
.toLowerCase();
}
toSnakeCase('camelCaseString'); // 'camel_case_string'
toSnakeCase('PascalCaseString'); // 'pascal_case_string'
toSnakeCase('kebab-case-string'); // 'kebab_case_string'
toSnakeCase('Title Case String'); // 'title_case_string'
// Handle acronyms:
toSnakeCase('parseHTTPRequest'); // 'parse_h_t_t_p_request' (naive)
// Better handling for acronyms:
function toSnakeCaseAdvanced(str) {
return str
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2') // Handle acronym transitions
.replace(/([a-z\d])([A-Z])/g, '$1_$2') // Handle camel transitions
.replace(/[\s-]+/g, '_')
.toLowerCase();
}
toSnakeCaseAdvanced('parseHTTPRequest'); // 'parse_http_request'
toSnakeCaseAdvanced('getHTML5Content'); // 'get_html5_content'
Python
import re
def to_snake_case(name):
# Handle transitions between uppercase sequences and titlecase
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
result = re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
# Replace spaces and hyphens
result = re.sub(r'[\s\-]+', '_', result)
return result
to_snake_case('camelCaseString') # 'camel_case_string'
to_snake_case('PascalCaseString') # 'pascal_case_string'
to_snake_case('parseHTTPRequest') # 'parse_http_request'
to_snake_case('kebab-case-string') # 'kebab_case_string'
# Using the inflection library:
# pip install inflection
import inflection
inflection.underscore('CamelCase') # 'camel_case'
inflection.underscore('getHTMLBody') # 'get_html_body'
Screaming snake case (SCREAMING_SNAKE_CASE)
UPPER_SNAKE_CASE (also called SCREAMING_SNAKE_CASE) is used for constants in many languages:
// JavaScript/TypeScript constants and environment variables:
const MAX_CONNECTIONS = 100;
const API_KEY = process.env.API_KEY;
const DEFAULT_PAGE_SIZE = 20;
// Enum values:
const Status = {
PENDING: 'pending',
ACTIVE: 'active',
INACTIVE: 'inactive',
};
# Python constants:
MAX_RETRIES = 3
DATABASE_HOST = "localhost"
SECRET_KEY = "your-secret-key-here"
Snake case vs other conventions
| Convention | Example | Used In |
|---|---|---|
| snake_case | user_name | Python, SQL, Ruby, file names |
| camelCase | userName | JavaScript, Java, C# variables |
| PascalCase | UserName | Classes in most languages |
| kebab-case | user-name | CSS, HTML attributes, URLs |
| SCREAMING_SNAKE | USER_NAME | Constants, environment variables |
When NOT to use snake_case
- JavaScript variables/functions — use camelCase (
userName, notuser_name) - React components — use PascalCase (
UserCard, notuser_card) - CSS classes — use kebab-case (
user-card, notuser_card) - URLs — use kebab-case (
/user-profile, not/user_profile)
Related tools
- Case Converter — convert between snake_case, camelCase, PascalCase, and more
- camelCase vs snake_case — when to use each
- Naming Conventions Guide — language-specific rules
Related posts
- camelCase — What It Is and Where to Use It in Programming — camelCase capitalizes the first letter of each word except the first: helloWorld…
- camelCase vs snake_case — Which Naming Convention to Use and When — camelCase and snake_case are the two dominant naming conventions in programming.…
- Kebab Case — What It Is and Where to Use kebab-case — Kebab case uses hyphens between lowercase words: hello-world, user-first-name. I…
- Programming Naming Conventions — camelCase, PascalCase, snake_case by Language — Different programming languages have different naming convention standards. Here…
Related tool
Convert text between camelCase, PascalCase, snake_case, kebab-case, SCREAMING_CASE, Title Case, sentence case, and more. Bulk mode.
Written by Mian Ali Khalid. Part of the Dev Productivity pillar.