🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

print()

AI & DATA SCIENCE // print

print() writes text and other objects to standard output (the console), converting each argument to a string and separating them with spaces by default.

Syntax

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Deep Dive Course

print() is the most common way to see what's happening inside a Python program. It accepts any number of positional arguments, converts each one with str(), joins them using the sep keyword (a single space by default), and finishes the line with the end keyword (a newline by default). The file argument redirects output elsewhere (like a log file), and flush forces the buffer to write immediately instead of waiting.

1Understanding print()

print() is the most common way to see what's happening inside a Python program. It accepts any number of positional arguments, converts each one with str(), joins them using the sep keyword (a single space by default), and finishes the line with the end keyword (a newline by default). The file argument redirects output elsewhere (like a log file), and flush forces the buffer to write immediately instead of waiting.

💡

Use f-strings (f"...") to build the message instead of passing many comma-separated arguments — it's easier to read and avoids surprises from the default space separator.

editor.html
name = "Alice"
age = 30
print(f"Name: {name}, Age: {age}")
localhost:3000

2Practical Example

Here is a real-world application of print() showing how it is used in production Python code.

editor.html
import time

for i in range(5):
    print(f"Loading... {i * 20}%")
    time.sleep(0.3)
print("Done!")
localhost:3000

3Best Practices

Follow these guidelines when working with print():

1. Prefer f-strings for formatting values instead of string concatenation with +

2. Set end='' when you need to build a line across multiple print() calls, e.g. a progress indicator

3. Use the logging module instead of print() for anything beyond quick, throwaway debugging

⚠️

Tip: Use f-strings (f"...") to build the message instead of passing many comma-separated arguments — it's easier to read and avoids surprises from the default space separator.

editor.html
name = "Alice"
age = 30
print(f"Name: {name}, Age: {age}")
localhost:3000

Examples

Example 01Basic Usage
name = "Alice"
age = 30
print(f"Name: {name}, Age: {age}")
Example 02Advanced Example
import time

for i in range(5):
    print(f"Loading... {i * 20}%")
    time.sleep(0.3)
print("Done!")

Best Practices

  • Prefer f-strings for formatting values instead of string concatenation with +
  • Set end='' when you need to build a line across multiple print() calls, e.g. a progress indicator
  • Use the logging module instead of print() for anything beyond quick, throwaway debugging

Interview Question

What do the sep and end keyword arguments of print() control, and what happens if you call print() with file=sys.stderr?

Hint: Think about what print() does between and after its arguments, and where output streams go.

sep controls the string inserted between multiple positional arguments (default a single space); end controls what's appended after the last argument (default a newline). Passing file=sys.stderr redirects that call's output to the standard error stream instead of standard output, which is useful for diagnostic messages that shouldn't mix with a program's normal output, e.g. when stdout is piped into another program.

Exercises

MediumPractice using print() in a real scenario.
View Solution
name = "Alice"
age = 30
print(f"Name: {name}, Age: {age}")

Frequently Asked Questions

What do the sep and end keyword arguments of print() control, and what happens if you call print() with file=sys.stderr?

sep controls the string inserted between multiple positional arguments (default a single space); end controls what's appended after the last argument (default a newline). Passing file=sys.stderr redirects that call's output to the standard error stream instead of standard output, which is useful for diagnostic messages that shouldn't mix with a program's normal output, e.g. when stdout is piped into another program.

Related Functions

input()f-stringslogging modulesys.stdout