How do I transform 10 line breaks in 1 in Python?


I have this text

Example 123

Example 456

and I have to transform into this:

Example 123
Example 456

Obs: 10 line breaks it’s just a example, can be any number (3,4,6…)

>Solution :

Since you have tagged this question with the regex tag, you can do

import re
text="""Example 123

Example 456
print(re.sub(r"\n{2,}", "\n", text))

which replaces all consecutive line breaks with one line break.

Leave a Reply