Python end (end=) Parameter in print()

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.

Benefits of end Parameter in Python

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.

Python end Parameter Syntax

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.

Python end Parameter Example

This section includes few examples that helps you, in understanding the topic completely. Let's start with very basic one, given below.

end Parameter with No Space and No Newline

This program uses end, that puts no space, and also no newline.

print("Py", end="")

The above program produces the output like shown in the snapshot given below:

python end

end Parameter with a Space and No Newline

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:

end parameter in python

end Parameter with a Comma, a Space, and No Newline

This is the third example program of end parameter. This program shows, how we can add multiple things using end:

print("Hello", end=", ")

This program produces:

end parameter example python

Note - Basically, whatever you provide inside the double quote of end="". You will get the respective output.

end Parameter with One, Two and Multiple Newlines

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:

python end parameter in print

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.

putting everything of print() inside end Parameter

You can also put all the things inside the end like shown in the program given below:

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.

end Parameter Provides Interactive Output for User Input

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:

python end example program

Use end Parameter to Print List Items in Single Line

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:

python end parameter example

Python Online Test

« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook

Subscribe Us on YouTube