0

我是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

[我想要标签而不是新行]

那么怎么可能呢?

4

2 回答 2

5

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

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

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

于 2013-01-06T16:28:08.887 回答
1

print 的默认行为是附加一个换行符:参见例如http://docs.python.org/3/whatsnew/3.0.html

于 2013-01-06T16:33:55.523 回答