Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 861
Ahmed A Avatar asked Apr 14 '26 09:04

Ahmed A


1 Answers

Not really.

>>> '     %-15s' % ('Hello',)
'     Hello          '
like image 151
Ignacio Vazquez-Abrams Avatar answered Apr 17 '26 00:04

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!