Created
November 7, 2022 21:14
-
-
Save gwerbin/97821b6934974a1c6e4c255ed14ab864 to your computer and use it in GitHub Desktop.
Indent lines of text in a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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