Python print() Function Explained: Newlines, Formatting, and Advanced Output Techniques

1. Basics of the Python print() Function

The Python print() function is the most fundamental tool for displaying output in a program. Here, we’ll explain the basic usage of print() and the essentials of newline behavior in its output.

The Role of the print() Function

In Python, the print() function is used to display output in the console. By default, each print() call automatically adds a newline after the output. Understanding this helps with formatting and debugging your programs. For example, the following code simply prints a string:
print("Hello, Python!")
Output:
Hello, Python!

Default Newline

The print() function includes a newline by default because its end parameter is set to '\n' (newline character).
print("Line 1")
print("Line 2")
Output:
Line 1
Line 2
As you can see, each print() statement automatically inserts a newline, so the results always appear on a new line.

2. Using the Newline Character \n

When you need to control line breaks explicitly, Python allows you to use the newline character \n. This lets you produce multi-line output within a single print() statement.

Example of Using \n for Line Breaks

For example, the following code uses \n to create multiple lines of output:
print("Line 1\nLine 2\nLine 3")
Output:
Line 1
Line 2
Line 3

Newlines with Lists and Dictionaries

You can also output lists and dictionaries using the print() function. If you want to display items vertically, using a for loop is very effective.
fruits = ["Apple", "Banana", "Orange"]
for fruit in fruits:
    print(fruit)
Output:
Apple
Banana
Orange

Advanced Example: Complex Data Structures

For multidimensional lists or nested dictionaries, recursive print() calls or formatting libraries (e.g., the pprint module) can be helpful for cleaner output.

3. Preventing Newlines – Using the end Parameter

Sometimes you may not want a newline after each print(). In such cases, the end parameter allows you to suppress newlines and combine outputs on a single line.

How to Use the end Parameter

The end parameter specifies the string appended after the output. By default, it is set to '\n', but you can customize it.
print("Hello", end=" ")
print("World!")
Output:
Hello World!

Practical Example: Suppressing Newlines in a for Loop

By suppressing newlines inside a for loop, you can combine values into a single line.
for i in range(5):
    print(i, end=", ")
Output:
0, 1, 2, 3, 4,

Advanced Example: Custom End Characters

You can specify any string as the end parameter. For example, it’s useful when creating comma-separated lists.
items = ["Apple", "Banana", "Orange"]
for item in items:
    print(item, end=", ")
Output:
Apple, Banana, Orange,

4. Multi-Line Strings with Quotes and Backslashes

To handle multi-line strings effectively, Python supports quotes and backslashes. These tools help keep code visually organized without changing the actual output.

Using Triple Quotes for Multi-Line Strings

Triple quotes (“”” or ”’) let you create multi-line strings, making it easy to output long text or multi-line content.
text = """This is the first line
This is the second line
This is the third line"""
print(text)
Output:
This is the first line
This is the second line
This is the third line

Formatting Code with Backslashes

When breaking up long code into multiple lines, you can use backslashes (\) to keep it readable, while the output remains a single line.
print("This is a very long string, but it will be printed as a single line without line breaks.")
Output:
This is a very long string, but it will be printed as a single line without line breaks.
RUNTEQ(ランテック)|超実戦型エンジニア育成スクール

5. Advanced Techniques: String Formatting and Newlines

In more advanced use cases, you can manage line breaks while formatting strings using the format() method or f-strings.

Formatting with the format() Method

The format() method allows you to insert variables into strings while keeping the output neatly formatted.
name = "Python"
version = 3.9
print("Language: {}\nVersion: {}".format(name, version))
Output:
Language: Python
Version: 3.9

Formatting with f-Strings

Starting from Python 3.6, f-strings provide a more concise way to insert variables into strings.
name = "Python"
version = 3.9
print(f"Language: {name}\nVersion: {version}")
Output:
Language: Python
Version: 3.9
年収訴求