Skip to content

Instantly share code, notes, and snippets.

@gwerbin
Created November 7, 2022 21:14
Show Gist options
  • Save gwerbin/97821b6934974a1c6e4c255ed14ab864 to your computer and use it in GitHub Desktop.
Save gwerbin/97821b6934974a1c6e4c255ed14ab864 to your computer and use it in GitHub Desktop.
Indent lines of text in a string
def indent_lines(text: str, spaces: int | str = 4) -> str:
r"""Indent lines of text by some fixed amount.
:param text: The text to indent.
:param spaces: The amount of spaces to indent by, or an arbitrary string to use as a
line prefix.
:returns: The indented text.
"""
if isinstance(spaces, int):
spaces = " " * spaces
return "".join([spaces + line for line in text.splitlines(True)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment