Online Tutorials & Training Materials | STechies.com
Register Login

Python : end parameter in print()

25 Mar 2020 10:48 am || 0

In this article, we will learn about the end parameter in python, it’s the default value and how to change the default value.

What is Python end Parameter?

The end parameter is used to append a string to the output of print() when printing of one statement is done. But ever wondered why after every print statement, the cursor moves to next line? This is because the print statement comes with a default value of end parameter that is ‘\n’.

‘\n’ is a special character sequence called an escape character. This escape character appends a newline after the print statement. Thus we get the output in the next line.

Let us understand this with the help of an example.

Example 1: Without Changing end Parameter Value

# Program to print statement
# With default value of end
print("Hello world")
print("Welcome to stechies")

Output:

Hello

Welcome to stechies

Explanation

In the above code, we printed two statements. But the 2nd statements was printed on the new line. This is because ‘end’ statement default value is ‘\n’ which. And thus the 2nd print statement is printed on the new line.

Example 2: Changing end Value to a White-space.

# Program to print statement without
# Using end statement
print("Hello world" , end = ' ') 
print("Welcome to stechies", end = ' ')

Output

Hello world Welcome to stechies

Explanation

In the above code, we changed the default value of ‘end’ statement from ‘\n’ to a white-space. Thus the 2nd print statement is not printed on the new line.

Example 3: Replacing end Default Value

# Program to print statement with
# end statement 
print("Hello world" , end = '@') 
print("Welcome to stechies", end = ' ')

Output

Hello world@Welcome to stechies

Explanation

In the above code, we replaced the default value of ‘end’ statement to ‘@’. Thus we got the output “Hello world@Welcome to stechies”.

Conclusion

The end parameter is used to append a new string to the output of the print function. But the default value of end parameter sends the cursor to the new line. Which we can change as per our requirement.