Sep in Python | Examples, and Explanation

Hello coders!! In this article, we will cover sep in Python. It may happen at times that we want to print formatted multiple values in a Python program. The sep argument in Python comes into play in such scenarios. Without wasting any time, let’s dive straight into the topic.

The sep parameter in Python:

Sep is a parameter in Python that primarily formats the printed statements on the output screen. Whitespace is the default value of this parameter. It adds a separator between strings to be printed. Let us see some examples to make our concept clear.

Syntax:

print(argument1, argument2, ..., sep = value)

Example 1: Python sep =”

print("Python", "Pool", sep = '')
Output Python sep =
Output

As we can see, when the value of sep is empty, there is no gap between the two statements.

Example 2: Python sep = ‘\n’

color=['red','blue','orange','pink']
print(*color, sep = ", ")  
print()
print(*color, sep = "\n") 
python sep =\n
Output

In this example, when we use the sep value ‘, ‘ the list’s values are printed in a comma-separated fashion. When the value of sep is ‘\n,’ i.e., newline, the list’s value is printed in a new line every time.

Example 3: Joining a list with a separator in Python

colors=['red','blue','orange','pink']
s="_".join(colors)
print(s)
red_blue_orange_pink

In this particular example, we first declared a list of colors containing four values: red, blue, orange, and pink. We then declared the sep value as ‘ _’. When we join the list using that separator, we can see that in the output, the value of the list is printed with the separator.

Example 4: Parsing a string in Python with sep

txt = "Python, a programming language, is easy to understand"
print(txt.split(", "))
['Python', 'a programming language', 'is easy to understand']

As we can see here, the value of the separator is a comma. As a result, the string is split at places where a comma is present in the sentence.

Difference between sep and end:

endsep
prints once all the values in the given print statement are printedseparates the print value by inserting the given value between them
Example:
st1=’python’
st2=’pool’
print(st1,st2,end=’%’)

Output:
python pool%
Example:
st1=’python’
st2=’pool’
print(st1,st2,sep=’%’)

Output:
python%pool

Conclusion:

With this, we come to the end of this article. The concept of sep for print statement formatting is relatively easy and simple. It finds major use in coding all over.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Dorian
Dorian
2 years ago

This article was very helpful, thank you !!

fiddy
fiddy
2 years ago

Perfectly explained! TY!

Pratik Kinage
Admin
1 year ago
Reply to  fiddy

Glad you liked it!