Camelcase in Python

Camelcase in Python

What Does Camelcase Mean?

Camelcase is a naming protocol for giving file or attribute names that contain more than one word joined that all start with a capital letter. Camelcase is a programming language that allows you to name files or functions without breaking the underlying language’s naming rules.

The name camelcase comes from its look, which resembles the back of a camel. Many programming languages that do not allow space in file names use it. Camelcase allows developers to create more distinctive and meaningful titles for them.

HelloWorld, Helloworld, and helloWorld, for example, are considerably easier to read than helloworld.

Ways to Convert String in Camelcase

Naive Method

These are the steps we’ll take to solve it. Make a blank string first. Then we’ll make the initial letter of each word in the string uppercase and the rest lowercase, and afterward, concatenate the words with s. Then, by lowering the initial letter, return the final string.

Code

Output:

helloWelcomeToPythonProgrammingInJavatpoint

Using re Module

In this, we will use sub of the re module

Code

Output:

original s1:  Python javatpoint
camelCase of s1:  pythonJavatpoint

original s2:  Python,javatpoint
camelCase of s2:  python,Javatpoint

original s3:  Python_javatpoint
camelCase of s3:  pythonJavatpoint

original s4:  python_javatpoint.tutorial-camelcase
camelCase of s3:  pythonJavatpoint.TutorialCamelcase

By Using split(), join(), title() and Generator Expression

This problem can be solved using a mix of the preceding functions. We divide all underscores initially; next, we will append the first word to the final string. We will proceed by title-cased words using the generator expression i.e. a for loop and title() function.

Code

Output:

The original string is : javatpoint_is_best_for_coding_tutorials
The camelcase of the string created is : javatpointIsBestForCodingTutorials

By Using split(), join(), title() and map()

This problem can be solved using a mix of the preceding functions. Using map(), we accomplish the goal of applying logic to full strings.

Code

Output:

The original string is : javatpoint_is_best_for_coding_tutorials
The camelcase of the created string is : javatpointIsBestForCodingTutorials

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263140.html

(0)
上一篇 2022年5月30日
下一篇 2022年5月30日

相关推荐

发表回复

登录后才能评论