当我输入'\ t'字符时,输出结果为换行符

时间:2013-01-06 16:24:04

标签: python tabs python-3.x newline

我是Python的新手,今天我正在基于列表在Python3.3上编写一个简单的测试程序。所以,我注意到当我输入制表空间字符\t时,输出被闪现,这样我就已经过了新行字符!下面给出了一个样本:

 def printlist(list_name,tabs=0):
         for items in list_name:
           if isinstance(items,list):
             printlist(items,tabs)
           else:
             print(items)
             for num in range(tabs):
                 print('\t') #tab-stops provide

    list3 = [
             'list no. 3',
             ['tree','stems','root'],
             ['human','hand','leg'],
             ['robot','microprocessor','motor']
            ]

   printlist(list3,1)

输出为:

    >>> ================================ RESTART ================================
>>> 
list no. 3


tree


stems


root


human


hand


leg


robot


microprocessor


motor


>>>

但输出格式我想要的是:

    list no. 3
    tree
    stems
    root
    human
    hand
    leg
    robot
    microprocessor
    motor

[ 我希望标签不是新行 ]

那怎么可能呢?

2 个答案:

答案 0 :(得分:5)

默认情况下,print()将以换行符结尾。如果要禁止此行为,请指定end

print("\t", end="")

文档在这里。 http://docs.python.org/3.3/library/functions.html#print

答案 1 :(得分:1)

print的默认行为是追加换行符:例如http://docs.python.org/3/whatsnew/3.0.html