Python打印缩进

时间:2017-04-05 13:12:06

标签: python string printing

在Python 3.x中如何打印缩进的空格,我正在寻找的方法看起来像这样

level = 3
print ('% indent symbol' % * level, 'indented text')

应打印:

            indented text

从下面重复回答:感谢@sudo_coffee的\ t

print ('\t' * level, 'indented text')

或者也许:

print ((' ' * 4) * level, 'indented text')

4 个答案:

答案 0 :(得分:4)

抱歉,伙计们,我想找到了答案,感谢@sudo_coffee的\ t

print ('\t' * level, 'indented text')

或者也许:

print ((' ' * 4) * level, 'indented text')

答案 1 :(得分:1)

缩进,或输入"标签"空间量,使用转义序列\t

level = 3
print ('\t indented text')

答案 2 :(得分:1)

level = 3
print (" "*level, 'indented text')

答案 3 :(得分:0)

level = 3
indented_symbol = "    "

print '{} Indented text'.format(indented_symbol*level)