Difference between end and sep in Python

In this article we will discuss the difference between The end and sep are two parameters in Python’s built-in print() function that will help to control how the output is formatted.

end in Python

The end is a parameter in Python’s built-in print() function that controls what character(s) are printed at the end of the printed text. 

  • The end is set to \n (newline character) which means that each call to print() will print the output on a new line. 
  • we can change the value of the end to print a different character or string at the end of the printed text.

Syntax :

print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)

Example :

Python




for i in range(1, 6):
    print i,


Output :

1 2 3 4 5

sep in Python

 the sep parameter is used to specify the separator between the values that are passed to the print() function. 

  • The print() uses a space character as the separator between values.

Syntax :

print(value1, value2, …, sep=’separator’)

Example :

Python




print("apple" + "-" + "banana" + "-" + "cherry")


Output :

apple-banana-cherry

Here’s an example that represents both the sep and end parameters in one line

Example :

Python3




print("Apples", "Orange", "Mango", sep=", ", end=" are fruits!\n")


Output:

Apples, Orange, Mango are fruits!

Difference Between end and sep in Python

Parameter Function Default Value  Usage Example
 
end  Specifies what should be printed at the end of the output.  ‘\n’ (newline character)  print(“Hello”, end=’!’)<br>Prints “Hello!” with an exclamation mark at the end.
 

sep

Specifies what separator character should be printed between multiple values. 

Space character (‘ ‘) 

print(“apple”, “banana”, “cherry”, sep=’-‘)<br>Prints “apple-banana-cherry” with hyphens between each value.

Conclusion

In Python, the sep parameter controls how items are separated within single print() statement in while the end parameter determines what is printed at the end of statement. These parameters provide flexibility in the formatting output according to your specific requirements.



Contact Us