6 Replies - 2446 Views - Last Post: 24 April 2011 - 07:52 AM Rate Topic: -----

#1 1cseanc1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-April 11

"end= " in Print Function

Posted 24 April 2011 - 06:13 AM

I would like to know what does the "end= " mean.

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



thanks~ ^_^
Is This A Good Question/Topic? 0
  • +

Replies To: "end= " in Print Function

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5959
  • View blog
  • Posts: 23,229
  • Joined: 23-August 08

Re: "end= " in Print Function

Posted 24 April 2011 - 06:17 AM

How about that manual?
Was This Post Helpful? 0
  • +
  • -

#3 Nallo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 161
  • View blog
  • Posts: 247
  • Joined: 19-July 09

Re: "end= " in Print Function

Posted 24 April 2011 - 06:20 AM

You might want to learn to use pythons inbuild help.
Open python 3.x shell and give it the command: help(print)
it will display:
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file: a file-like object (stream); defaults to the current sys.stdout.
    sep:  string inserted between values, default a space.
    end:  string appended after the last value, default a newline.


I hope this need no explanation.
Was This Post Helpful? 0
  • +
  • -

#4 1cseanc1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-April 11

Re: "end= " in Print Function

Posted 24 April 2011 - 06:31 AM

I get it. But when in this complete module, I'm still digesting it:

# This module prints out nested lists

def crack(listname, level):
     for deeper_list in listname:
          if isinstance(deeper_list, list):
               crack(deeper_list, level+1)

          else:
               for l in range(level):
                    print("\t", end=' ')
               print(deeper_list)

#This will print out the nested list with tab-stops according to the level.
# E.g. numbers = [1, 2, 3, [4, 5]]
"""

result:

1
2
3
     4
     5


Was This Post Helpful? 0
  • +
  • -

#5 Nallo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 161
  • View blog
  • Posts: 247
  • Joined: 19-July 09

Re: "end= " in Print Function

Posted 24 April 2011 - 07:04 AM

Thats simple. For each level you are in:
for l in range(level):
    print("\t", end=' ')


prints an according number of tabs and stays in the same line. If you left out the end=' ' each time a newline would be printed and the tabs would be lost (the next print then starts at the beginning of the new line). (BTW. I would prefer end='')

Edit: end='' means end=' ' with the space inbetween removed. Might be hard to read here.

Edit2: Maybe your problem with understanding is not the print syntax, but the recursion the module uses?

This post has been edited by Nallo: 24 April 2011 - 07:14 AM

Was This Post Helpful? 1
  • +
  • -

#6 1cseanc1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-April 11

Re: "end= " in Print Function

Posted 24 April 2011 - 07:28 AM

Okay now I understand~ ^_^ thx
Was This Post Helpful? 0
  • +
  • -

#7 Nallo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 161
  • View blog
  • Posts: 247
  • Joined: 19-July 09

Re: "end= " in Print Function

Posted 24 April 2011 - 07:52 AM

Sorry to ask, but was the hard part to understand the recursion? Or was it really the print syntax?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1