1 2 3 4 5
def reindent(s, numSpaces): s = string.split(s, '\n') s = [(numSpaces * ' ') + string.lstrip(line) for line in s] s = string.join(s, '\n') return s
When working with text, it may be necessary to change the indentation level of a block. This code will take a multiline string and add or remove leading spaces to each line so that the indentation level of the block matches some absolute number of spaces.
>>> x = """line one ... line two ... and line three ... """ >>> print x line one line two and line three >>> print reindent(x, 8) line one line two and line three </pre>