Controlling Output of Python print(): How to Use sep and end Arguments

Python’s print() function is frequently used to check execution results or variable contents. By default, print() separates multiple arguments with a space and automatically adds a newline at the end.

However, sometimes you might want to change this separator (for example, to create CSV format) or prevent the newline (to show progress on a single line). This article explains two important optional arguments for customizing print() output: sep and end.


目次

sep: Specifying the Separator

When you pass multiple arguments to the print() function, they are separated by spaces by default.

item_code = "A-001"
quantity = 50
location = "Warehouse-3"

# Default (space separated)
print(item_code, quantity, location)

Output:

A-001 50 Warehouse-3

By specifying the sep (separator) argument, you can change this delimiter to anything you like. For example, use it when you want to separate data with a pipe symbol (|) or a comma (,).

# Specify sep='|'
print(item_code, quantity, location, sep="|")

# Specify sep=',' (for CSV format, etc.)
print(item_code, quantity, location, sep=",")

Output:

A-001|50|Warehouse-3
A-001,50,Warehouse-3

end: Specifying the End Character

The print() function adds a newline character (\n) at the end of the output by default. Therefore, if you call print() consecutively, the output will span multiple lines.

print("--- Start ---")
print("Verifying data...")
print("--- Done ---")

Output:

--- Start ---
Verifying data...
--- Done ---

By specifying the end argument, you can change this ending character. Specifically, if you specify end="" (an empty string), the newline is suppressed, and the output of the next print() will continue on the same line. This is useful when you want to display process progress on one line.

print("--- Start ---", end="")
print("Verifying data...", end="")
print("--- Done ---")

Output:

--- Start ---Verifying data...--- Done ---

You can also specify a space for end to make it easier to read.

print("Loading:", end=" ")
modules = ["Core", "Graphics", "Network"]
for mod in modules:
    print(f"[{mod}]", end=" ") # Output each module separated by space

print("...Done") # Newline at the end

Output:

Loading: [Core] [Graphics] [Network] ...Done

Using sep and end Together

You can specify sep and end at the same time. For example, if you want to join multiple log pieces with hyphens and close with a specific marker (like [EOF]) instead of a newline:

log_level = "INFO"
module_name = "AuthService"
message = "Login successful"

# Specify separator with sep, and end character with end
print(log_level, module_name, message, sep=" - ", end=" [END_LOG]\n")

Output:

INFO - AuthService - Login successful [END_LOG]

Summary

By using the sep and end arguments of the print() function, you can flexibly control the output format.

  • sep: Specifies the separator character when multiple arguments are passed. (Default: space)
  • end: Specifies the character at the end of the output. (Default: \n newline)

Use these arguments to achieve the desired output format for logging, data formatting, or progress display.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次