I'd like to add some characters to the beginning of each line.
How could I do ?
I was doing that:
'\n\t\t\t'.join(myStr.splitlines())
But it isn't perfect and I'd like to know if there are better ways to do that. I originally want to indent a whole block of text automatically.
For flexible option, you may wish to have a look at textwrap in the standard library.
Example:
>>> hamlet='''\
... To be, or not to be: that is the question:
... Whether 'tis nobler in the mind to suffer
... The slings and arrows of outrageous fortune,
... Or to take arms against a sea of troubles,
... And by opposing end them? To die: to sleep;
... No more; and by a sleep to say we end
... '''
>>> import textwrap
>>> wrapper=textwrap.TextWrapper(initial_indent='\t', subsequent_indent='\t'*2)
>>> print wrapper.fill(hamlet)
To be, or not to be: that is the question: Whether 'tis nobler in the
mind to suffer The slings and arrows of outrageous fortune, Or to
take arms against a sea of troubles, And by opposing end them? To
die: to sleep; No more; and by a sleep to say we end
You can see that you can not only easily add flexible space on the front of each line, you can trim each line to fit, hyphenate, expand tabs, etc.
It will wrap (hence the name) lines that become too long because of the additions on the front:
>>> wrapper=textwrap.TextWrapper(initial_indent='\t'*3,
... subsequent_indent='\t'*4, width=40)
>>> print wrapper.fill(hamlet)
To be, or not to be: that is the
question: Whether 'tis nobler in the
mind to suffer The slings and arrows
of outrageous fortune, Or to take
arms against a sea of troubles, And
by opposing end them? To die: to
sleep; No more; and by a sleep to
say we end
Very flexible and useful.
Edit
If you wish to keep the meaning of line endings in the text with textwrap, just combine textwrap with splitlines to keep line endings the same.
Example of hanging indent:
import textwrap
hamlet='''\
Hamlet: In the secret parts of Fortune? O, most true! She is a strumpet. What's the news?
Rosencrantz: None, my lord, but that the world's grown honest.
Hamlet: Then is doomsday near.'''
wrapper=textwrap.TextWrapper(initial_indent='\t'*1,
subsequent_indent='\t'*3,
width=30)
for para in hamlet.splitlines():
print wrapper.fill(para)
print
prints
Hamlet: In the secret parts
of Fortune? O, most true!
She is a strumpet. What's
the news?
Rosencrantz: None, my lord,
but that the world's grown
honest.
Hamlet: Then is doomsday
near.
I think that's a pretty nice method. One thing you could improve is that your method introduces a leading newline, and removes any trailing newline. This won't:
'\t\t\t'.join(myStr.splitlines(True))
From the docs:
str.splitlines([keepends])
Return a list of the lines in the string, breaking at line boundaries. This method uses the universal newlines approach to splitting lines. Line breaks are not included in the resulting list unless keepends is given and true.
Also, unless your string begins with a newline, you aren't adding any tabs at the beginning of the string, so you may want to do this too:
'\t\t\t'.join(('\n'+myStr.lstrip()).splitlines(True))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With