I am having trouble figuring out this code, I have tried everything: here it is: snake_cased_char_list = [
‘_’ + char.lower() if char.isupper() else char
for char in pascal_or_camel_cased_string
]
Your code so far
def convert_to_snake_case(pascal_or_camel_cased_string):
# snake_cased_char_list = []
# for char in pascal_or_camel_cased_string:
# if char.isupper():
# converted_character = '_' + char.lower()
# snake_cased_char_list.append(converted_character)
# else:
# snake_cased_char_list.append(char)
# snake_cased_string = ''.join(snake_cased_char_list)
# clean_snake_cased_string = snake_cased_string.strip('_')
# return clean_snake_cased_string
# User Editable Region
snake_cased_char_list = [
'_' + char.lower() if char.isupper() else char
for char in pascal_or_camel_cased_string
]
# User Editable Region
return ''.join(snake_cased_char_list).strip('_')
def main():
print(convert_to_snake_case('aLongAndComplexString'))
main()
this is PascalCase: NameOfFunction and this is camelCase: nameOfFunction, but snake_case is like this: name_of_function, so in order to obtain a snake_case, we need to have all words in lower case separated but "" in place of white space, so the whole function is about:
snake_cased_char_list = ['’ + char.lower() if char.isupper() else char for char in pascal_or_camel_cased_string]
so we need to convert the upper letters to lower after an “_”, and this is the condition.
In general, a comprehensive list is made like the following:
[EXPRESSION1 if CONDITION else EXPRESSION2 for ITERATOR in ITERABLE] which is equivalent of:
for ITERATOR in ITERABLE: “for example, for i in range(10)”
if CONDITION:
lst.append(EXPRESSION1)
else:
lst.append(EXPRESSION2)