- Python Basics
- Python Tutorial
- Python Applications
- Python Versions
- Python environment setup
- Python Basic Syntax
- Python end (end=)
- Python sep (sep=)
- Python Comments
- Python Identifiers
- Python Variables
- Python Operators
- Python Ternary Operator
- Python Operator Precedence
- Python Control and Decision
- Python Decision Making
- Python if elif else
- Python Loops
- Python for Loop
- Python while Loop
- Python break Statement
- Python continue Statement
- Python pass Statement
- Python break vs. continue
- Python pass vs. continue
- Python Built-in Types
- Python Data Types
- Python Lists
- Python Tuples
- Python Sets
- Python frozenset
- Python Dictionary
- List vs. Tuple vs. Dict vs. Set
- Python Numbers
- Python Strings
- Python bytes
- Python bytearray
- Python memoryview
- Python Misc Topics
- Python Functions
- Python Variable Scope
- Python Enumeration
- Python import Statement
- Python Modules
- Python operator Module
- Python os Module
- Python Date and Time
- Python Exception Handling
- Python File Handling
- Python Advanced
- Python Classes and Objects
- Python @classmethod Decorator
- Python @staticmethod Decorator
- Python Class vs. Static Method
- Python @property Decorator
- Python Keywords
- Python Keywords
- Python and
- Python or
- Python not
- Python True
- Python False
- Python None
- Python in
- Python is
- Python as
- Python with
- Python yield
- Python return
- Python del
- Python from
- Python lambda
- Python assert
- Python Built-in Functions
- Python All Built-in Functions
- Python print() Function
- Python input() Function
- Python int() Function
- Python len() Function
- Python range() Function
- Python str() Function
- Python ord() Function
- Python chr() Function
- Python read()
- Python write()
- Python open()
- Python Examples
- Python Examples
Python sep (sep=) parameter in print() with examples
Using the value given to the "sep" parameter in print(), you can separate more than one parameter. For example, the following Python code
print("s", "e", "p")
produces the following output:
As you can see from the above output, there is a single space inserted automatically between each parameter (that is, "s," "e," and "p"). But using the "sep" parameter, we can remove the default behavior of print() and insert an automatic space between each of its parameters. Here is the modified version of the previous program:
print("s", "e", "p", sep="")
Now, the output of this program will be:
Important: The "sep=" parameter must be placed as the last parameter of print().
Use of the sep parameter
The use of the sep parameter is not limited to only removing automatically inserted spaces between all parameters of print(). But it can also be used to insert any required character or combination of characters between all parameters, as shown in the program given below:
print("My Computer", "C Drive", "Program Files", sep=" -> ")
This program produces the output as shown in the snapshot given below:
Use of the escape sequence using the sep parameter
You can also use escape sequence characters using the sep parameter in print(). To illustrate it, first let's take an example without assigning an escape sequence to sep. Here is the program:
print("s ", "e ", "p", sep="")
The above program produces:
Now let's create the same program with an escape sequence character, say "\b" (the backslash character), assigned to sep, created after modifying the above program:
print("s ", "e ", "p", sep="\b")
This time, the program produces the following output:
Note: These are some demo programs showing you the use of sep. Or how this parameter plays an important role sometimes when you need these types of outputs.
You can also format the date or anything you need using the sep parameter in the required way, like shown in the demo program given below:
print("01", "09", "2021", sep="-")
This program produces the following output:
01-09-2021
Advantages of the sep in Python
- Customization: The "sep" parameter lets you customize the separator between printed values. You can change the separator from space to any character or string.
- Flexibility: The "sep" parameter is very flexible and can print multiple values with different separators. This helps format data for printing.
- Readability: By separating values with a character or string, the "sep" parameter makes output more readable. This simplifies program output parsing.
Disadvantages of the sep in Python
- Python 3.x and later support the "sep" parameter. This parameter won't work in older Python versions.
- Complexity: The "sep" parameter can complicate code if you're unfamiliar with it. To get the desired result, you must know how to use it.
- Maintenance: If you change your output format, remember to update your "sep" parameter. Maintenance can be difficult, especially in larger projects.
« Previous Tutorial Next Tutorial »