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 to 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 in 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 joined 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 the places where there is a presence of a comma in the sentence.

Difference between sep and end:

endsep
prints once all the values in the given print statement is 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 an end to 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!

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments