How to use 'sep' in Python

Learn to use the sep parameter in Python. This guide covers different methods, tips, real-world applications, and how to debug common errors.

Published on: 
Fri
Feb 20, 2026
Updated on: 
Mon
Apr 6, 2026
The Replit Team

The sep parameter in Python's print() function helps you format output. It's a simple way to control spacing between items for clean, readable code.

In this article, you'll explore key techniques for the sep parameter. You'll get practical tips, see real-world applications, and receive debugging advice to help you master its use for professional output.

Basic usage of sep in print()

print("Hello", "World", sep="-")
names = ["Alice", "Bob", "Charlie"]
print(*names, sep=", ")--OUTPUT--Hello-World
Alice, Bob, Charlie

The sep parameter lets you define a custom separator for the items you pass to the print() function. In the first example, sep="-" replaces the default single space, joining "Hello" and "World" into a single connected string.

The second example demonstrates a more dynamic application. Using the splat operator (*) on the names list unpacks its elements into individual arguments for print(). The sep=", " parameter then inserts a comma and a space between each name, creating a clean, human-readable list without needing a manual loop.

Basic sep techniques

The sep parameter's flexibility extends beyond simple characters, allowing you to work with various data types and join strings with custom or even empty separators.

Using different separators in print()

print("Python", "is", "fun", sep=" ") # Default space separator
print("Python", "is", "fun", sep="-") # Hyphen separator
print("Python", "is", "fun", sep="\t") # Tab separator--OUTPUT--Python is fun
Python-is-fun
Python is fun

The sep parameter accepts any string, letting you precisely control your output's format. While sep=" " replicates the default single-space behavior, you can use other strings for different effects.

  • Using sep="-" connects items with a hyphen, which is great for creating simple, joined strings.
  • Using sep="\t" inserts a tab character, a handy trick for aligning your output into columns without complex formatting.

Using sep with multiple data types

print(42, 3.14, True, "Python", sep=" | ")
print("ID:", 1001, "Status:", False, sep="→")--OUTPUT--42 | 3.14 | True | Python
ID:→1001→Status:→False

The print() function automatically handles different data types. Before joining items with the separator, it converts every argument into its string representation. This means you don't have to manually cast integers, floats, or booleans to strings.

  • In the first example, the integer 42, float 3.14, and boolean True are all converted to strings and then joined with " | ".
  • The second example shows a practical use for creating structured output by mixing labels like "ID:" with values like 1001.

Using empty separator for string joining

words = ["auto", "mobile"]
print(*words, sep="")
print("a", "b", "c", "d", "e", "f", sep="")--OUTPUT--automobile
abcdef

By setting sep="", you're telling the print() function to join its arguments with nothing in between. This provides a quick way to concatenate strings or individual characters without adding any spaces.

  • The first example unpacks the words list, and sep="" merges "auto" and "mobile" into a single word.
  • Similarly, the second example joins the individual letters to form a continuous string.

This technique is a memory-efficient concise alternative to using the "".join() method for simple concatenation tasks directly within a print statement.

Advanced sep applications

With the fundamentals down, you can now apply the sep parameter to more advanced scenarios involving dictionary operations, unpacked iterables, and special escape sequences.

Using sep with dictionary operations

person = {"name": "Alice", "age": 30}
print(*person.keys(), sep=", ")
print(*person.values(), sep=", ")
print(*person.items(), sep="\n")--OUTPUT--name, age
Alice, 30
('name', 'Alice')
('age', 30)

The sep parameter is particularly useful when combined with dictionary methods. By using the splat operator (*), you can unpack the contents of a dictionary's keys, values, or items directly into the print() function for formatted output.

  • print(*person.keys(), sep=", ") unpacks the keys and joins them into a clean, comma-separated string.
  • print(*person.values(), sep=", ") achieves the same result for the dictionary's values.
  • Using sep="\n" with *person.items() is a great way to display each key-value pair on its own line for easy inspection.

Using sep with unpacked iterables

numbers = range(1, 6)
print(*numbers, sep=" - ")
letters = "PYTHON"
print(*letters, sep="/")--OUTPUT--1 - 2 - 3 - 4 - 5
P/Y/T/H/O/N

The splat operator (*) isn't just for lists; it works on any iterable. You can unpack objects like a range or a string directly into print(), letting sep format the output.

  • In the first example, *numbers expands the range object into individual numbers, and sep=" - " places a separator between each one.
  • Strings are also iterables, so *letters breaks "PYTHON" into its component characters, which are then joined by sep="/".

Working with escape sequences in sep

print("Column1", "Column2", "Column3", sep="\t")
print("Row1", "Row2", "Row3", sep="\n")--OUTPUT--Column1 Column2 Column3
Row1
Row2
Row3

Escape sequences like \t (tab) and \n (newline) give you powerful formatting control within the sep parameter. They aren't printed literally; instead, they instruct the console how to arrange the output.

  • Using sep="\t" inserts a tab between each argument, which is perfect for creating neatly aligned columns of data.
  • Setting sep="\n" places each argument on a new line, effectively turning a single print() call into a multi-line display.

Move faster with Replit

Replit is an AI-powered development platform that comes with all Python dependencies pre-installed, so you can skip setup and start coding instantly. While mastering techniques like the sep parameter is useful, Agent 4 helps you move from piecing together code to building complete applications.

Instead of piecing together techniques, describe the app you want to build and Agent 4 will take it from idea to working product. For example, you could build:

  • A tag generator that converts a list of keywords into a single, comma-separated string for blog posts.
  • A log formatter that combines data from multiple lists into a structured, readable output for debugging.
  • A data exporter that takes user profiles and formats them into a custom delimited string for easy migration.

Simply describe your app, and Replit will write the code, test it, and fix issues automatically, all within your browser.

Common errors and challenges

While the sep parameter is straightforward, you might encounter a few common issues—but they're all simple to resolve.

  • Troubleshooting when sep doesn't affect string contents
  • Fixing type errors with the sep parameter
  • Resolving issues when using sep with file output

Troubleshooting when sep doesn't affect string contents

A common snag is when the sep parameter doesn't seem to work. You might expect it to separate the characters inside a single string, but it only applies when you pass multiple arguments to print(). The code below shows a classic example.

word = "Python"
print(word, sep="-") # Won't separate the letters

The print() function sees word as a single item, so the sep parameter has no other arguments to separate. To apply the separator between each letter, you must pass each character as a separate argument. The code below shows how to achieve this.

word = "Python"
print(*word, sep="-")

The fix is to use the splat operator (*). When you write print(*word, sep="-"), you're not passing the string word as one item. Instead, you're unpacking it into individual characters: 'P', 'y', 't', 'h', 'o', 'n'. Now, print() sees multiple arguments, and the sep parameter can correctly insert the separator between each one. Keep an eye out for this when you want to format the contents of any single iterable.

Fixing type errors with the sep parameter

Fixing type errors with the sep parameter

The sep parameter is particular about its data type—it only accepts strings. If you try to use a number or any other non-string value, Python will stop and raise a TypeError. The code below shows exactly what this looks like.

items = ["apple", "banana", "cherry"]
print(*items, sep=3) # TypeError: sep must be str, not int

The error occurs because sep=3 attempts to use an integer as a separator, but the sep parameter strictly requires a string. See how to resolve this type mismatch in the corrected code below.

items = ["apple", "banana", "cherry"]
print(*items, sep="3")

The fix is simple: always provide a string to the sep parameter. The TypeError occurs because sep=3 passes an integer, but the function expects a string. By wrapping the number in quotes, as in sep="3", you provide the correct data type. This is a good reminder to double-check the type of any variable you use as a separator, as it must always be a string.

Resolving issues when using sep with file output

The sep parameter is exclusive to the print() function and won't work with file-writing methods like write(). This common mix-up leads to a TypeError because write() expects a single string argument, not multiple items and a separator. The code below shows this error in action.

with open("data.txt", "w") as f:
data = ["apple", "banana", "cherry"]
f.write(*data, sep=", ") # TypeError: write() takes exactly one argument

The splat operator (*) unpacks the list into separate arguments. Since the f.write() method expects a single string, not multiple items, it raises a TypeError. Check the code below for the correct approach to writing formatted data to a file.

with open("data.txt", "w") as f:
data = ["apple", "banana", "cherry"]
f.write(", ".join(data))

The f.write() method doesn't support a sep parameter, so you must first join your data into a single string. The correct approach is using the ", ".join(data) method, which creates one formatted string that f.write() can accept. Remember this distinction whenever you're writing multiple data elements to a file, as the sep parameter is a feature exclusive to the print() function.

Real-world applications

With common errors resolved, you can apply the sep parameter to practical tasks like building file paths and formatting database query results.

Creating file paths with sep

You can also use the sep parameter to dynamically construct file paths, which is particularly useful since different operating systems use different separators.

windows_dirs = ["C:", "Users", "Username", "Documents", "file.txt"]
unix_dirs = ["", "home", "username", "documents", "file.txt"]
print(*windows_dirs, sep="\\")
print(*unix_dirs, sep="/")

This code shows how to construct a single string from a list of path components. The splat operator (*) unpacks each list, feeding its elements as separate arguments into the print() function. The sep parameter then joins these arguments together.

  • For the windows_dirs list, sep="\\" is used. You'll need the double backslash because a single backslash is an escape character in Python strings.
  • For the unix_dirs list, sep="/" simply joins the items with a forward slash.

This is a concise way to assemble a complete string from an iterable, making it perfect for vibe coding scenarios.

Formatting database results with sep

The sep parameter is also perfect for turning raw database query results into a clean, human-readable table.

query_result = [
(1, "Alice", "alice@example.com", "Active"),
(2, "Bob", "bob@example.com", "Inactive"),
(3, "Charlie", "charlie@example.com", "Active")
]

print("ID", "Name", "Email", "Status", sep=" | ")
print("-" * 60)
for user in query_result:
print(*user, sep=" | ")

This example combines a for loop with the splat operator (*) to format a list of tuples into a clean table. The code first prints a header row, then a decorative line, before processing each record.

  • The loop iterates through each tuple in query_result.
  • Inside the loop, *user unpacks the tuple, feeding its contents to print() as separate arguments.

This allows the sep parameter to place a " | " between each data point, creating neatly aligned columns that match the header.

Get started with Replit

Turn your knowledge of the sep parameter into a real tool. Describe what you want to build to Replit Agent, like "a CSV data formatter" or "a script that creates custom-delimited log entries".

Replit Agent writes the code, tests for errors, and deploys your app, turning your instructions into a finished product. Start building with Replit.

Build your first app today

Describe what you want to build, and Replit Agent writes the code, handles the infrastructure, and ships it live. Go from idea to real product, all in your browser.

Build your first app today

Describe what you want to build, and Replit Agent writes the code, handles the infrastructure, and ships it live. Go from idea to real product, all in your browser.