Home » Python » Python programs

Convert a String to camelCase in Python

Here, we are going to learn how to convert a String to camelCase in Python programming language?
Submitted by IncludeHelp, on March 27, 2020

Here, we are implementing a python program to convert a given string to camelCase.

Example of camelCase:

    String: "Hello world"
    camelCase string: "helloWorld"

Program:

# importing the module
from re import sub

# function to convert string to camelCase
def camelCase(string):
  string = sub(r"(_|-)+", " ", string).title().replace(" ", "")
  return string[0].lower() + string[1:]

# main code
s1 = "Hello world"
s2 = "Hello,world"
s3 = "Hello_world"
s4 = "hello_world.txt_includehelp-WEBSITE"

print("s1: ", s1)
print("camelCase(s1): ", camelCase(s1))
print()

print("s2: ", s2)
print("camelCase(s2): ", camelCase(s2))
print()

print("s3: ", s3)
print("camelCase(s3): ", camelCase(s3))
print()

print("s4: ", s4)
print("camelCase(s4): ", camelCase(s4))
print()

Output

s1:  Hello world
camelCase(s1):  helloWorld

s2:  Hello,world
camelCase(s2):  hello,World

s3:  Hello_world
camelCase(s3):  helloWorld

s4:  hello_world.txt_includehelp-WEBSITE
camelCase(s4):  helloWorld.TxtIncludehelpWebsite





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.