在python中打印一个字符串,左边用偏移量对齐

Ahm*_*d A 3 python string-formatting

我使用的是Python 2.4.我想打印一个左对齐但带有"偏移"的字符串.我的意思是,在它之前打印一个带有一定数量空格的字符串.

例:

在宽度为20的空格中打印字符串"Hello",左对齐,但在字符串之前插入五个空格.

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

print "Hello".ljust(20) #does not cut it.  
Run Code Online (Sandbox Code Playgroud)

我可以使用以下作为解决方法:

print "     ", "Hello".ljust(15)
Run Code Online (Sandbox Code Playgroud)

有没有比打印5个字符串更好的方法.

艾哈迈德,谢谢你.

Ign*_*ams 8

并不是的.

>>> '     %-15s' % ('Hello',)
'     Hello          '
Run Code Online (Sandbox Code Playgroud)