Python Practice › String Programs › Program 88
Space before each capital letter
Write a Python program to insert a space before every capital letter.
Input
"SqaTools pyThon"
Output
' Sqa Tools py Thon'
Approach
- Use
re.subto put a space before each uppercase letter.
Solution
import reprint(repr(re.sub(r"([A-Z])", r" \1", "SqaTools pyThon")))Output
' Sqa Tools py Thon'
Line by line
Line 2
\1 puts the captured capital back after the inserted space.
Learn the concept
New to this topic? Read the lesson, then come back.