Home > front end >  Python function to convert camelCase to Sentence Case
Python function to convert camelCase to Sentence Case

Time:12-22

I want to convert my camelCaseSentence to Sentence Case.

Example:

String: 'sarimChaudharyIsMyName'

So the output of this should be...

String: 'Sarim Chaudhary Is My Name'

CodePudding user response:

Here is a simple regex approach using the help of the title() function:

inp = 'sarimChaudharyIsMyName'
output = re.sub(r'(?<=[a-z])(?=[A-Z])', ' ', inp).title()
print(output)  # Sarim Chaudhary Is My Name

We can maybe improve performance a bit by avoiding the title() call and instead just uppercasing the first character:

inp = 'sarimChaudharyIsMyName'
output = re.sub(r'(?<=[a-z])(?=[A-Z])', ' ', inp)
output = output[0].upper()   output[1:]
print(output)  # Sarim Chaudhary Is My Name

CodePudding user response:

def camel_to_Sentence_Case(a):
    temp = a[0].upper()
    for i in a[1:]:
        if i.isupper():
            temp = ' ' i
        else:
            temp =i
    
    return temp
  • Related