Write a function that takes a string, and outputs that string formatted in camelcase. (alternating upper and lower case for each character, e.g. cAmElCaSe)

A simple approach would be something like the following:def camelcase(text): camel_chars = [] # start an empty list where we'll store the components of the new string for i in range(len(text)): # for each character if i % 2 == 0: # check if odd or even camel_chars.append(text[i].lower()) # cast to lowercase else: camel_chars.append(text[i].upper()) # cast to uppercase return ''.join(camel_chars) # join the list together into a new string without spaces We build the new string up from a list of characters instead of appending to a string, because lists are generally mutable and easier to work with, but you could equally well use string concatenation inside the loop and it would work just as well in most cases.

Answered by Andrey B. Python tutor

980 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

How might we implement the bubble sort algorithm in Python


Create a python code to sum the number from 1 to 10.


Write a simple number guessing game, give the user 3 tries to guess a number between 1 and 10


Explain why creating a list of several instances of one element using something like my_list = [a] * 5 can result in strange behaviour


We're here to help

contact us iconContact usWhatsapp logoMessage us on Whatsapptelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2024

Terms & Conditions|Privacy Policy