This article is created to cover an interesting topic in Python, that is end parameter of print(). The end (end=) can be seen in many programs of Python. Therefore we must have to understand about it.
The end parameter is used to change the default behavior of print() statement in Python. That is, since print() automatically inserts a newline after each execution. Therefore, using end this can change. The end parameter is basically used to customized or sometime interactive output.
Of course, we can use end to skip insertion of an automatic newline. But we can also use it to insert one, two, or multiple newlines using one single print(). The examples given below, shows the use of end in every aspect.
Since end is an parameter of print() statement/function, therefore there is nothing unique in its syntax. That is, the syntax to use end looks like:
Inside the double quote, you can insert whatever you want, according to your need such as a space, no space, comma, newline (\n) etc.
This section includes few examples that helps you, in understanding the topic completely. Let's start with very basic one, given below.
This program uses end, that puts no space, and also no newline.
print("Py", end="") print("thon")
The above program produces the output like shown in the snapshot given below:
The program given below shows how to add a single or multiple spaces with no newline in a Python program:
print("This is a tutorial on", end=" ") print("\"end\" parameter.")
The snapshot given below shows the sample output produced by above program:
This is the third example program of end parameter. This program shows, how we can add multiple things using end:
print("Hello", end=", ") print("Programmer")
This program produces:
Note - Basically, whatever you provide inside the double quote of end="". You will get the respective output.
Since print() statement by default, always inserts/prints a single newline. But using end as its parameter, we can change this to print required number of newlines like shown in the program given below:
print("Hey", end="!\n\n") print("What's Up", end="\n") print("Great to see you learning Python here.", end="\n\n\n\n") print("Thank You!")
Now the output produced by this Python program is exactly same as shown in the snapshot given below:
You can also put some message/content of print() inside its end parameter like shown in the program given below:
print("H", end="ey!\n\n") print("What", end="'s Up\n") print("Great to see you ", end="learning Python here.\n\n\n\n") print("Thank", end=" You!\n")
This program produces the same output as of previous program's output.
You can also put all the things inside the end like shown in the program given below:
print(end="Hey!\n\n") print(end="What's Up\n") print(end="Great to see you learning Python here.\n\n\n\n") print(end="Thank You!\n")
Again this program also produces the same output as of second previous program.
Note - Basically the end parameter of print() forces it, not to automatically insert a newline.
This type of program, where the end parameter is used when asking to enter some inputs from user, may be seen a lot of times, to receive the input on the same line, where the asking message is written like shown in the program given below:
print("Enter anything: ", end="") val = input() print("You've entered:", val)
Here is its sample run with user input codescracker:
This is the task, where we almost want to use end every time. Because when we've a list of large number of elements, then it takes large number of lines, that looks weird sometime. Therefore using end, we can modify it like shown in the program given below:
nums = [1, 2, 3, 4, 5] for n in nums: print(n, end=" ")
This program produces:
© Copyright 2021. All Rights Reserved.