w3resource

Python: Insert spaces between words starting with capital letters


Write a Python program to insert spaces between words starting with capital letters.

Sample Solution:

Python Code:

import re
def capital_words_spaces(str1):
  return re.sub(r"(\w)([A-Z])", r"\1 \2", str1)

print(capital_words_spaces("Python"))
print(capital_words_spaces("PythonExercises"))
print(capital_words_spaces("PythonExercisesPracticeSolution"))

Sample Output:

Python
Python Exercises
Python Exercises Practice Solution 

Pictorial Presentation:

Python: Insert spaces between words starting with capital letters.
Python: Insert spaces between words starting with capital letters.

Flowchart:

Flowchart: Regular Expression - Insert spaces between words starting with capital letters.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to remove the parenthesis area in a string.
Next: Write a Python program that reads a given expression and evaluates it.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.