[python] 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.

This question is related to python python-3.x python-3.3

The answer is


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 +


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.


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to python-3.x

Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation Replace specific text with a redacted version using Python Upgrade to python 3.8 using conda "Permission Denied" trying to run Python on Windows 10 Python: 'ModuleNotFoundError' when trying to import module from imported package What is the meaning of "Failed building wheel for X" in pip install? How to downgrade python from 3.7 to 3.6 I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? Iterating over arrays in Python 3 How to upgrade Python version to 3.7?

Examples related to python-3.3

Meaning of end='' in the statement print("\t",end='')? Division in Python 2.7. and 3.3 SyntaxError: multiple statements found while compiling a single statement How to install pip for Python 3 on Mac OS X? Random word generator- Python How to make program go back to the top of the code instead of closing Writing to CSV with Python adds blank lines Python 3: ImportError "No Module named Setuptools"