Python Practice › String Programs › Program 88

Space before each capital letter

EasyString Programs · 88 of 106

Write a Python program to insert a space before every capital letter.

Advertisement
Input
"SqaTools pyThon"
Output
' Sqa Tools py Thon'

Approach

  1. Use re.sub to put a space before each uppercase letter.

Solution

Python
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.

More string programs

← All 106 String Programs

Leave a Comment