Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of end='' in the statement print("\t",end='')? [duplicate]

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.

like image 834
Rajath Avatar asked Dec 05 '14 09:12

Rajath


People also ask

What is meaning of end =' in Python?

The end parameter in the print function is used to add any string. At the end of the output of the print statement in python. By default, the print function ends with a newline. Passing the whitespace to the end parameter (end=' ') indicates that the end character has to be identified by whitespace and not a newline.

What does end do in the print () statement?

The end key of print function will set the string that needs to be appended when printing is done. By default the end key is set by newline character. So after finishing printing all the variables, a newline character is appended.

What does Sep \t mean?

sep="\t" tells R that the file is tab-delimited (use " " for space delimited and "," for comma delimited; use "," for a . csv file).

How do you end a print in Python 2?

Python end parameter in print() Python's print() function comes with a parameter called 'end'. By default, the value of this parameter is 'n', i.e. the new line character. You can end a print statement with any character/string using this parameter.


2 Answers

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 +

like image 66
Bhargav Rao Avatar answered Oct 22 '22 22:10

Bhargav Rao


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.

like image 4
RemcoGerlich Avatar answered Oct 22 '22 23:10

RemcoGerlich