Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
List comprehension or a for loop, which is better? solution in Clear category for Conversion into CamelCase by lucas.stonedrake
#1st solution
def to_camel_case(name):
result = [] # create empty list
for w in name.split('_'): #split by underscore
result += w.capitalize() #capitalise each word
return ''.join(result) # join the list entries together
# using list comprehension, split by underscore and capitalise
def to_camel_case(name):
return ''.join([w.capitalize() for w in name.split('_')])
if __name__ == '__main__':
print("Example:")
print(to_camel_case('name'))
#These "asserts" using only for self-checking and not necessary for auto-testing
assert to_camel_case("my_function_name") == "MyFunctionName"
assert to_camel_case("i_phone") == "IPhone"
assert to_camel_case("this_function_is_empty") == "ThisFunctionIsEmpty"
assert to_camel_case("name") == "Name"
print("Coding complete? Click 'Check' to earn cool rewards!")
Nov. 6, 2020