Ahmed A
Ahmed A

Reputation: 3652

print a string in python left justified with an offset

I am using Python 2.4. I would like to print a string left justified but with an "offset". By that I mean, print a string with a set number of spaces before it.

Example:

Print string "Hello" in a space of width 20, left justified, but five spaces inserted before the string.

"     Hello          "   #(The string has 5 spaces prior, and 10 space after)

print "Hello".ljust(20) #does not cut it.  

I could use the following as a workaround:

print "     ", "Hello".ljust(15)

Is there a better approach than printing a string of 5 spaces.

Thank you, Ahmed.

Upvotes: 3

Views: 4618

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

Not really.

>>> '     %-15s' % ('Hello',)
'     Hello          '

Upvotes: 9

Related Questions