Meaning of end=” in the statement print(“t”,end=”)?

This is the function for printing all values in a nested list (taken from Head first with Python).

def printall(the_list, level):
    for x in the_list:
        if isinstance(x, list):
            printall(x, level=level + 1)
        else:
            for tab_stop in range(level):
                print("t", end='')
        print(x)

The function is working properly.

The function basically prints the values in a list and if there is a nested list then it print it by a tab space.

Just for a better understanding, what does end=' ' do?

I am using Python 3.3.5

For 2.7

f =  fi.input( files = 'test2.py', inplace = True, backup = '.bak')
for line in f:
    if fi.lineno() == 4:
        print line + 'n'
        print 'extra line'
    else:
        print line + 'n'

as of 2.6 fileinput does not support with.
This code appends 3 more lines and prints the appended text on the 3rd new line. and then appends a further 16 empty lines.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The default value of end is n meaning that after the print statement it will print a new line. So simply stated end is what you want to be printed after the print statement has been executed

Eg: – print ("hello",end=" +") will print hello +

Method 2

See the documentation for the print function: print()

The content of end is printed after the thing you want to print. By default it contains a newline ("n") but it can be changed to something else, like an empty string.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x