- Case Styles: Camel, Pascal, Snake, and Kebab Case
- Elegant Python function to convert CamelCase to snake_case?
The most popular ways to combine words into a single string
- camelCase
- PascalCase
- snake_case
- kebab-case
Case Styles | Text |
Raw | user login count |
Camel Case | userLoginCount |
Pascal Case | UserLoginCount |
Snake Case | user_login_count |
Snake Case (All Caps) | USER_LOGIN_COUNT |
Kebab Case | user-login-count |
Snake case to camel case
def snake_to_camel(text): text = ''.join(word.title() for word in text.split('_')) return text st = 'user_login_count' print(snake_to_camel(st)) # UserLoginCount
Camel case to snake case
- docs.python.org –
re
— Regular expression operations
import re def camel_to_snake(text): return re.sub(r'(?<!^)(?=[A-Z])', '_', text).lower() print(camel_to_snake('userLoginCount')) print(camel_to_snake('HTTPResponseCodeXYZ')) # user_login_count # h_t_t_p_response_code_x_y_z
import re def camel_to_snake(name): name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower() def to_snake_case(name): name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) name = re.sub('__([A-Z])', r'_\1', name) name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name) return name.lower() print(camel_to_snake('UserLoginCount')) print(camel_to_snake('camel2_camel2_case')) print(camel_to_snake('getHTTPResponseCode')) print(camel_to_snake('HTTPResponseCodeXYZ')) # user_login_count # camel2_camel2_case # get_http_response_code # http_response_code_xyz