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 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:
| Input | One-liner | Expected |
|---|---|---|
userName | user_name | user_name |
parseHTTPResponse | parse_h_t_t_p_response | parse_http_response |
getUserID | get_user_i_d | get_user_id |
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.
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.
The Case Converter converts names one per line to snake_case, camelCase, PascalCase, kebab-case or CONSTANT_CASE, keeping acronyms and digits together.
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 ConverterConverting a name is easy. Renaming it everywhere in a file is where things break. A find-and-replace will also change:
"userName" in an error message or a JSON keyresponse.userName, which belongs to another objectfetch(pageSize=pageSize); the left side is the callee's parameter name{ totalCount } in JavaScript is shorthand for { totalCount: totalCount }, so renaming it changes your APIIn 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.
| Language | Variables & functions | Classes | Constants |
|---|---|---|---|
| Python | snake_case | PascalCase | CONSTANT_CASE |
| JavaScript / TypeScript | camelCase | PascalCase | CONSTANT_CASE or camelCase |
| Java | camelCase | PascalCase | CONSTANT_CASE |
| C / C++ | project-dependent | project-dependent | CONSTANT_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.