Guide October 29, 2026 · 6 min read

How to Convert camelCase to snake_case (Python, JavaScript & Online)

Converting userName to user_name is easy. Converting parseHTTPResponse correctly — and renaming variables inside real code without breaking it — takes a little more care. Here is how to do both.

The one-liner everyone uses (and where it fails)

The most common Python answer inserts an underscore before every capital letter:

import re

def camel_to_snake(name):
    return re.sub(r'(?<!^)(?=[A-Z])', '_', name).lower()

That works for userName → user_name, but splits acronyms letter by letter:

InputOne-linerExpected
userNameuser_nameuser_name
parseHTTPResponseparse_h_t_t_p_responseparse_http_response
getUserIDget_user_i_dget_user_id

Python: handle acronyms with two passes

Split before a capitalised word first, then between a lowercase letter or digit and a capital:

import re

def camel_to_snake(name):
    name = re.sub(r'(.)([A-Z][a-z]+)', r'\1_\2', name)
    return re.sub(r'([a-z0-9])([A-Z])', r'\1_\2', name).lower()

camel_to_snake("parseHTTPResponse")  # 'parse_http_response'
camel_to_snake("getUserID")          # 'get_user_id'
camel_to_snake("base64Encode")       # 'base64_encode'

And back again:

def snake_to_camel(name):
    first, *rest = name.split('_')
    return first + ''.join(word.title() for word in rest)

snake_to_camel("max_retry_count")  # 'maxRetryCount'

If you'd rather use a library, inflection.underscore() and inflection.camelize(name, False) from the inflection package do the same.

JavaScript

const camelToSnake = (name) =>
  name
    .replace(/([A-Z]+)([A-Z][a-z])/g, "$1_$2")
    .replace(/([a-z0-9])([A-Z])/g, "$1_$2")
    .toLowerCase();

const snakeToCamel = (name) =>
  name.replace(/_([a-z0-9])/g, (_, c) => c.toUpperCase());

camelToSnake("parseHTTPResponse"); // "parse_http_response"
snakeToCamel("max_retry_count");   // "maxRetryCount"

Lodash's _.snakeCase() and _.camelCase() also work. Note that Lodash treats numbers as separate words, so check names like base64Encode.

Online: convert a list of names

The Case Converter converts names one per line to snake_case, camelCase, PascalCase, kebab-case or CONSTANT_CASE, keeping acronyms and digits together.

Convert camelCase to snake_case online

Paste names one per line, pick a case, copy. Or switch to Code mode to rename variables in real code. 5 free tries, no account needed.

Open the Case Converter

Renaming variables inside real code

Converting a name is easy. Renaming it everywhere in a file is where things break. A find-and-replace will also change:

In an IDE, use rename refactoring (F2 in VS Code, Shift+F6 in JetBrains IDEs), which understands scope. For a snippet, the Case Converter's Code mode renames variables while skipping strings, attributes, keyword arguments and object keys, and lists anything it skipped so you can decide.

Which convention should you use?

LanguageVariables & functionsClassesConstants
Pythonsnake_casePascalCaseCONSTANT_CASE
JavaScript / TypeScriptcamelCasePascalCaseCONSTANT_CASE or camelCase
JavacamelCasePascalCaseCONSTANT_CASE
C / C++project-dependentproject-dependentCONSTANT_CASE

Whatever the language, consistency inside a project matters more than the convention itself — which is also why AI-generated code that mixes styles stands out in review. The Code Humanizer can bring it in line.