Printing with indentation in python

Question:

is there a way to print the following,

print user + ":tt" + message

so that lengthy messages that are wider than the length of the terminal always wraps (starts from the same position) ?
so for example this

Username:              LEFTLEFTLEFTLEFTLEFTLEFTLEFT
RIGHTRIGHTRIGHT

should become

Username:              LEFTLEFTLEFTLEFTLEFTLEFTLEFT
                       RIGHTRIGHTRIGHT
Asked By: Algorithmatic

||

Answers:

You can use str.ljust() to pad out each line to the required width like so:

line_width = 20
print "Username:".ljust(line_width) + "LEFT"*6
print "".ljust(line_width) + "RIGHT"*3

The argument you pass to ljust is the length you wish the string to be, as long as this is consistant the lines should line up correctly.

Alternately, you can use string multiplication on lines where you just need the paddding like so:

print " "*line_width + "RIGHT"*3

This will have the exact same output as the last line in the above code.

Answered By: user764357

I think what you’re looking for here is the textwrap module:

user = "Username"
prefix = user + ": "
preferredWidth = 70
wrapper = textwrap.TextWrapper(initial_indent=prefix, width=preferredWidth,
                               subsequent_indent=' '*len(prefix))
message = "LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT " * 3
print wrapper.fill(message)

This prints:

Username: LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT
          LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT
          LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT

If you actually want to use tabs in the indent, that’s a little trickier, because you have to first tab-expand the initial_indent to figure out the correct subsequent_indent to use. And, because your prefix actually ends with two tabs, it’s even more complicated. Here’s the simplest I’ve come up with:

user = "Username"
prefix = user + ":tt"
expanded_indent = textwrap.fill(prefix+'$', replace_whitespace=False)[:-1]
subsequent_indent = ' ' * len(expanded_indent)
wrapper = textwrap.TextWrapper(initial_indent=prefix,
                               subsequent_indent=subsequent_indent)
message = "LEFTLEFTLEFTLEFTLEFTLEFTLEFT RIGHTRIGHTRIGHT " * 3
print wrapper.fill(message)

If you do this repeatedly, you will probably want to wrap that mess up in a function.

Answered By: abarnert

In Python 2, I suggest using format to indent left e.g.:

print '{:<30}{:<40}'.format('UserName:','Foo')
print '{:<30}{:<40}'.format('User:','FooBar')
print '{:<30}{:<40}'.format('','FooBar42')

will result to:

UserName:                     Foo                                     
User:                         FooBar                                  
                              FooBar42     

And so on…

For Python 3, use parentheses 🙂
Also, the same can be written more compact using the f-strings:

print (f"{'UserName:':<30}{'Foo':<40}")
print (f"{'User:':<30}{'FooBar':<40}")
print (f"{'':<30}{'FooBar42':<40}")

will also result to:

UserName:                     Foo                                     
User:                         FooBar                                  
                              FooBar42     
Answered By: ntg

you can also use this format:
let’s say you have the data frame df that you want to print all of it with an indentation.

print("t"+df.to_string().replace("n","nt"))

should work like magic

Answered By: Eilay Koren
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.