X Xerobit

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...

Mian Ali Khalid · · 4 min read
Use the tool
Case Converter
Convert text between camelCase, PascalCase, snake_case, kebab-case, SCREAMING_CASE, Title Case, sentence case, and more. Bulk mode.
Open Case Converter →

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

ConventionExampleUsed In
snake_caseuser_namePython, SQL, Ruby, file names
camelCaseuserNameJavaScript, Java, C# variables
PascalCaseUserNameClasses in most languages
kebab-caseuser-nameCSS, HTML attributes, URLs
SCREAMING_SNAKEUSER_NAMEConstants, environment variables

When NOT to use snake_case

  • JavaScript variables/functions — use camelCase (userName, not user_name)
  • React components — use PascalCase (UserCard, not user_card)
  • CSS classes — use kebab-case (user-card, not user_card)
  • URLs — use kebab-case (/user-profile, not /user_profile)

Related posts

Related tool

Case Converter

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.