I have this text: ""Anna lives in Latin America.\n\nShe loves the vibes from the cities\n and the good weather.\n\n\nAnna is great" And I want to reduce the x newlines into x-1 newlines. So the expected result is: "Anna lives in Latin America.\nShe loves the vibes from the cities and the good weather.\n\nAnna is great" I tried this: import re def clean_text(text): text = re.sub(r'\n{2,}', '\n', text) return text result = clean_text("Anna lives in Latin America.\n\nShe loves the vibes from the cities\n and the good weather.\n\n\nAnna is great") result But it returns "Anna lives in Latin America.\nShe loves the vibes from the cities\n and the good weather.\nAnna is great", which not what I expected. Continue reading...