我正在使用Python 2.4。我想要左对齐打印字符串,但是带有“偏移量”。我的意思是,在字符串之前打印一组固定数量的空格。
例如:在20个字符的宽度中打印字符串“Hello”,左对齐,但在字符串之前插入五个空格。
我可以使用以下方法作为解决办法:
有没有比打印5个空格更好的方法?
谢谢, Ahmed。
例如:在20个字符的宽度中打印字符串“Hello”,左对齐,但在字符串之前插入五个空格。
" Hello " #(The string has 5 spaces prior, and 10 space after)
print "Hello".ljust(20) #does not cut it.
我可以使用以下方法作为解决办法:
print " ", "Hello".ljust(15)
有没有比打印5个空格更好的方法?
谢谢, Ahmed。
%('Hello',)
而不是%("Hello")
? - mgilson('hello',)
是一个包含1个元素的元组;('hello')
则是一个字符串。 - Maxy-B