Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
2 solutions: isupper() and regex solution in Clear category for Conversion from CamelCase by H0r4c3
def from_camel_case_(name: str) -> str:
new_name = name[0].lower() + name[1:]
for item in new_name:
if item.isupper():
new_name = new_name.replace(item, '_' + item.lower())
return new_name
import re
def from_camel_case(name: str) -> str:
name = name[0].lower() + name[1:]
regex = '([a-z])([A-Z])'
#print(re.sub(regex, r'\1_\2', name).lower())
return re.sub(regex, r'\1_\2', name).lower()
if __name__ == '__main__':
print("Example:")
print(from_camel_case("Name"))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert from_camel_case("MyFunctionName") == "my_function_name"
assert from_camel_case("IPhone") == "i_phone"
assert from_camel_case("ThisFunctionIsEmpty") == "this_function_is_empty"
assert from_camel_case("Name") == "name"
print("Coding complete? Click 'Check' to earn cool rewards!")
Dec. 17, 2021
Comments: