Camelcase Naming Convention in Programming

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Camelcase is a naming convention where a compound word or phrase is written in a way that the first word is in lowercase and the subsequent words are capitalized. This naming convention is commonly used in programming languages to name variables, functions, and classes.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/FileHandlingGroup(["`File Handling`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/FileHandlingGroup -.-> python/with_statement("`Using with Statement`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") subgraph Lab Skills python/comments -.-> lab-13062{{"`Camelcase Naming Convention in Programming`"}} python/with_statement -.-> lab-13062{{"`Camelcase Naming Convention in Programming`"}} python/lists -.-> lab-13062{{"`Camelcase Naming Convention in Programming`"}} python/tuples -.-> lab-13062{{"`Camelcase Naming Convention in Programming`"}} python/function_definition -.-> lab-13062{{"`Camelcase Naming Convention in Programming`"}} python/importing_modules -.-> lab-13062{{"`Camelcase Naming Convention in Programming`"}} python/using_packages -.-> lab-13062{{"`Camelcase Naming Convention in Programming`"}} python/standard_libraries -.-> lab-13062{{"`Camelcase Naming Convention in Programming`"}} end

Camelcase String

Problem

You are given a string that may contain spaces, hyphens, or underscores. Your task is to convert the string to camelcase by removing the spaces, hyphens, or underscores and capitalizing the first letter of each word except the first one. The first letter of the resulting string should be in lowercase.

Example

camel('some_database_field_name') ## 'someDatabaseFieldName'
camel('Some label that needs to be camelized')
## 'someLabelThatNeedsToBeCamelized'
camel('some-javascript-property') ## 'someJavascriptProperty'
camel('some-mixed_string with spaces_underscores-and-hyphens')
## 'someMixedStringWithSpacesUnderscoresAndHyphens'

Summary

In this challenge, you learned how to convert a string to camelcase by removing spaces, hyphens, or underscores and capitalizing the first letter of each word except the first one. You used re.sub() to replace any - or _ with a space, using the regexp r"(_|-)+", str.title() to capitalize the first letter of each word and convert the rest to lowercase, and str.replace() to remove spaces between words.

Other Python Tutorials you may like