1

I'm new in Python, today I was writing a simple test program on Python3.3 based on list. So, I've noticed that when I was entering tab space character \t, the output was flashed such that I had enetered new line character! A sample is given below:

 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)

And the output is:

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


tree


stems


root


human


hand


leg


robot


microprocessor


motor


>>>

But the output format what I intended is:

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

[I want tabs not a new line]

So how will it be possible?

biswajit
  • 2,707
  • 4
  • 17
  • 16
  • What should this code really do? Take all list items as a flat list and output each indidivual list item with N tabs indented? –  Jan 06 '13 at 16:26
  • 1
    Print always prints a newline. See this question: http://stackoverflow.com/questions/5598181/python-print-on-same-line – Chris Jan 06 '13 at 16:27

2 Answers2

5

By default, print() will end with a newline. If you want to suppress this behavior specify end.

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

Documentation is here. http://docs.python.org/3.3/library/functions.html#print

recursive
  • 83,943
  • 34
  • 151
  • 241
  • OP also doesn't increment `tabs` in recursive calls, but this is the biggest issue with the output. –  Jan 06 '13 at 16:28
  • Maybe string multiplication is worth explaining: `print("\t" * tabs, end="")` – utdemir Jan 06 '13 at 16:34
  • I'M GETTING SYNTAX ERROR when I'm using: end="" – biswajit Jan 06 '13 at 16:48
  • In order to figure out what you're doing wrong, you'll need to include enough code to reproduce the syntax error. – recursive Jan 06 '13 at 16:49
  • @Tinctorius: The desired output has unchanging indentation. There should be no need to increment anything. – recursive Jan 06 '13 at 16:55
  • I have placed print("\t", end="") insted of print("\t") but I'm getting syntax error – biswajit Jan 06 '13 at 17:22
  • I think it was some fault in execution, its now working, thanks – biswajit Jan 06 '13 at 17:34
  • but I'm getting error (syntax error); I don't understand what's going wrong – biswajit Jan 06 '13 at 17:41
  • @BiswajitPaul: `print("\t", end="")` is not a syntax error in python 3.3. I don't know what your problem is, but in order to give you any more information, like I said earlier, you'll have to include enough information to reproduce the problem. – recursive Jan 06 '13 at 18:41
  • 1
    @recursive actually I have installed both of the python 3.3 and 2.7. So when I've pressed f5 in my editor, the python shell window 2.7 pops up automatically. That's why I was getting error. – biswajit Jan 06 '13 at 19:10
  • @BiswajitPaul [Since Python 2.6](https://docs.python.org/2/library/__future__.html), you can use `from __future__ import print_function` to make it compatible with Python 3. And don't forget to accept an answer. – Cees Timmerman Feb 27 '15 at 11:50
1

The default behavior of print is to append a newline: see e.g. http://docs.python.org/3/whatsnew/3.0.html

grep
  • 726
  • 7
  • 13