Fundamentals 5 min read

How to Split CamelCase Strings into Readable Words in Python

This article explains how to transform a CamelCase string like "WeLovePython" into a spaced, properly capitalized sentence in Python, presenting five different code solutions—including loop‑based, regex, and string‑method approaches—and compares their implementations.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Split CamelCase Strings into Readable Words in Python

Introduction

In a recent Python community discussion, a user asked how to convert a CamelCase string such as "WeLovePython" into a spaced sentence where the first word starts with a capital letter and the rest are lowercase.

Implementation

Several solutions were shared.

# coding:utf-8

text = "WeLovePython"
res = ''.join([" " + i if i.isupper() else i for i in text]).strip()
print(res)

This code inserts a space before each uppercase letter and then removes the leading space, producing "We Love Python". However, it leaves the original capitalization.

text = "WeLovePython"
res = ''.join([" " + i if i.isupper() else i for i in text]).strip()
print(res)
b = res[0] + ''.join([i.lower() for i in res[1:]])
print(b)

The second snippet adds a step that keeps the first character unchanged and lowercases the rest, yielding "We love python".

text = "WeLovePython"
res = text[0] + ''.join([" " + i if i.isupper() else i for i in text[1:]]).lower()
print(res)

res = ''.join([" " + i if i.isupper() else i for i in text]).strip().capitalize()
print(res)

Another approach uses a similar loop but applies lower() to all characters after the first one.

text = "WeLovePython"
res = ''.join([" " + i if i.isupper() else i for i in text]).strip()
res1 = res[:1] + res[1:].lower()
print(res1)

A regex‑based solution was also provided:

import re

text = "WeLovePython"
result = re.sub(r'\w[A-Z]', lambda x: ' '.join(x.group(0).lower()), text)
print(result)

This uses a regular expression to find a lowercase‑uppercase pattern and replace it with a space followed by the lowercase version.

Conclusion

The article presents five methods that all achieve the same goal: splitting a CamelCase string into separate words with appropriate capitalization. Readers are encouraged to try these approaches and share alternative solutions.

Code examplesstring-manipulationCamelCase
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.