Вы можете сделать отступ строк в строке, просто заполнив каждую из них нужным количеством символов пэда. Это легко сделать с помощью функции textwrap.indent()
, которая была добавлена в модуль в Python 3.3. В качестве альтернативы вы можете использовать приведенный ниже код, который также будет работать в более ранних версиях Python.
try:
import textwrap
textwrap.indent
except AttributeError: # undefined function (wasn't added until Python 3.3)
def indent(text, amount, ch=' '):
padding = amount * ch
return ''.join(padding+line for line in text.splitlines(True))
else:
def indent(text, amount, ch=' '):
return textwrap.indent(text, amount * ch)
text = '''\
And the Lord God said unto the serpent,
Because thou hast done this, thou art
cursed above all cattle, and above every
beast of the field; upon thy belly shalt
thou go, and dust shalt thou eat all the
days of thy life: And I will put enmity
between thee and the woman, and between
thy seed and her seed; it shall bruise
thy head, and thou shalt bruise his
heel.
3:15-King James
'''
print('Text indented 4 spaces:\n')
print(indent(text, 4))
Результат:
Text indented 4 spaces:
And the Lord God said unto the serpent,
Because thou hast done this, thou art
cursed above all cattle, and above every
beast of the field; upon thy belly shalt
thou go, and dust shalt thou eat all the
days of thy life: And I will put enmity
between thee and the woman, and between
thy seed and her seed; it shall bruise
thy head, and thou shalt bruise his
heel.
3:15-King James