w3resource

Python: Insert spaces between words starting with capital letters

Python Regular Expression: Exercise-51 with Solution

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/re/python-re-exercise-51.php