EN
Python - convert camelCase to sentence case
0 points
In this article, we would like to show you how to convert camelCase to sentence case in Python.
In this example, we use re.sub()
function to replace every capital letter with the same letter in lowercase followed by a space character. Then with string slicing we convert whole string to the lowercase except the first letter which we convert to the uppercase.
xxxxxxxxxx
1
import re
2
3
4
def sentence_case(string):
5
if string != '':
6
result = re.sub('([A-Z])', r' \1', string)
7
return result[:1].upper() + result[1:].lower()
8
return
9
10
11
text = "mySampleText"
12
print(sentence_case(text))
Output:
xxxxxxxxxx
1
My sample text