代码如下
# -*- coding: utf-8 -*- def get_lower_case_name(text): lst = [] for index, char in enumerate(text): if char.isupper() and index != 0: lst.append("_") lst.append(char) return "".join(lst).lower() if __name__ == '__main__': print(get_lower_case_name("StudentNameModel")) # student_name_model
2021-04-16补充一种实现方式
# -*- coding: utf-8 -*- import re def pascal_case_to_snake_case(camel_case: str): """大驼峰(帕斯卡)转蛇形""" snake_case = re.sub(r"(?P<key>[A-Z])", r"_\g<key>", camel_case) return snake_case.lower().strip('_') def snake_case_to_pascal_case(snake_case: str): """蛇形转大驼峰(帕斯卡)""" words = snake_case.split('_') return ''.join(word.title() for word in words)