Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Substitution and MULTILINE - remove leading spaces

import re

text = """  First row
Second row
  Third row
"""

print(text)
print('-----')
print(re.sub(r'^\s+', '', text))
print('-----')
print(re.sub(r'\A\s+', '', text, flags=re.MULTILINE))
print('-----')
print(re.sub(r'^\s+', '', text, flags=re.MULTILINE))

Output:

  First row
Second row
  Third row

-----
First row
Second row
  Third row

-----
First row
Second row
  Third row

-----
First row
Second row
Third row