Regex in Python to Put Spaces Between Words Starting with Capital Letters (original) (raw)

Last Updated : 4 Nov, 2025

Given a string where words are joined together without spaces and each word starts with a capital letter, the task is to insert spaces between the words and convert all letters to lowercase.

**For Example:

**Input: GeeksForGeeks
**Output: geeks for geeks

Let’s explore different methods to insert spaces between words starting with capital letters in Python.

Using re.sub()

"re.sub()" method substitutes each capital letter (except the first one) with a space before it and then converts the whole string to lowercase.

Python `

import re s = "GeeksForGeeks" res = re.sub(r'(?<!^)(?=[A-Z])', ' ', s).lower() print(res)

`

**Explanation:

Using re.findall() and join()

"re.findall()" method extracts all words starting with an uppercase letter and then joins them with spaces after converting each to lowercase.

Python `

import re s = "GeeksForGeeks" w = re.findall(r'[A-Z][a-z]*', s) res = ' '.join(word.lower() for word in w) print(res)

`

**Explanation:

Using re.split()

"re.split()" method splits the string before every uppercase letter and then joins the parts with spaces after converting them to lowercase.

Python `

import re s = "GeeksForGeeks" p = re.split(r'(?=[A-Z])', s) res = ' '.join(part.lower() for part in p if part) print(res)

`

**Explanation: