Home > Mobile >  What is the point of including print() at the end of a Python function?
What is the point of including print() at the end of a Python function?

Time:09-30

def display_list(self):
    if self.start is None:
        print("List is empty!")

    print("List is: ")
    p = self.start
    while p is not None:
        print(p.info, " ", end=" ")
        p = p.next
    print()

This code simply prints the list's contents. As you can see, there is a print() function at the end of the function with no arguments. What is the point of including that, and is it actually needed?

CodePudding user response:

    x = 5 
    print(x,end="\n",sep=" ")

The end attribute of print functions which is by default set to newline is responsible for adding new line after print statement is over.You can change this to achieve any other functionality.

    print("a",end="<=>",sep=" ")//outputs a<=> 

CodePudding user response:

To make the next print statement after the function starts from the next line. Compare these codes:

def fn():
    i = 0
    while i < 10:
        print(i, end=' ')
        i  = 1

fn()
print('hello world')

output:

0 1 2 3 4 5 6 7 8 9 hello world

and this:

def fn():
    i = 0
    while i < 10:
        print(i, end=' ')
        i  = 1
    print()

fn()
print('hello world')

output:

0 1 2 3 4 5 6 7 8 9 
hello world
  • Related