Regex in Python to Put Spaces Between Words Starting with Capital Letters

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)

Output
geeks for geeks

Explanation:

  • re.sub(r'(?<!^)(?=[A-Z])', ' ', s) inserts a space before each capital letter that is not at the start of the string ((?<!^) is a negative lookbehind).
  • lower() converts the entire string to lowercase.

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)

Output
geeks for geeks

Explanation:

  • re.findall(r'[A-Z][a-z]*', s) extracts each word starting with an uppercase letter followed by lowercase letters.
  • list comprehension converts each extracted word to lowercase.
  • ' '.join() joins all words with spaces to form the final sentence.

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)

Output
geeks for geeks

Explanation:

  • re.split(r'(?=[A-Z])', s) splits the string before every uppercase letter.
  • condition "if part" removes any empty strings created at the start.
  • each part is converted to lowercase and joined with spaces.
Comment