Print Function Python: Arguments and Output

A Python program may need to display a value while it runs. The built-in print() function converts positional objects to text and writes them to a text stream, usually the terminal.

I’ll show how to choose separators and line endings for that output.

TL;DR: Python print()

Python print() writes text for people or programs to read, and its arguments control separators, line endings, and destination.

  • Pass one or more positional objects to display them.
  • Use sep between objects and end after them.
  • By default, output goes to sys.stdout and print() returns None.

What Is the Python print() Function?

print() is a built-in function that converts each positional object to text and writes the result to a text stream. The default stream is sys.stdout, usually the terminal.

The function accepts zero or more objects, followed by keyword-only sep, end, file, and flush options. The default separator is a space, while the default ending is a newline. For string work beyond displaying values, see Python string functions.

Calling print() with no objects writes its end value, a newline by default. Use it to add a blank line. The expression print(“”, end=””) writes no visible characters and adds no line break.

Lists and dictionaries appear in their ordinary string representation, which can help inspect values. That representation is not a defined interchange format for another program.

Optional settings follow positional objects and use their names. For example, print(“red”, “blue”, sep=”/”) inserts a slash between values.

A formatted string suits a sentence with embedded values. Separate objects suit a delimiter between values. The official built-in function reference specifies accepted forms.

How to Use Python print() Step by Step

Start with positional values, then choose the text between them and after the final value. I ran the complete example below to check what appears and what the call returns.

Step 1: Print objects and set the separator

Give print() separate values when you want it to convert each one. The sep string replaces the default space between those values.

name = "Asha"
score = 18
print("Student:", name)
print("Score:", score)
print("Status", "ready", sep=" | ", end="!\n")
print("done")
print("print returns", print("done"))

The exact output is:

Student: Asha
Score: 18
Status | ready!
done
done
print returns None
Terminal output from print_demo.py showing values, separator, and None return

The separator appears between the two Status values, and end adds an exclamation mark before the newline. The nested call displays done, but its return value is None, which explains the final line.

Step 2: Choose a line ending

Use end when successive print() calls should share a line or finish with a different marker. The demo’s end=”!\n” places the next call on a fresh line after the exclamation mark.

Step 3: Send text to a file

Pass a text file object as file to redirect output, and open it with a context manager so Python closes it after writing.

with open("report.txt", "w", encoding="utf-8") as report:
    print("Run complete", file=report)

The file contains Run complete followed by a newline. The file-handling guide explains opening and reading text files in more detail: Python file handling.

print() Edge Cases and Fixes

Keep sep and end as strings. I passed an integer separator and got the error shown below.

print("start", sep=1)
TypeError: sep must be None or a string, not int
Python traceback for an integer passed as print sep

Pass a string such as sep=” | “. The Python reference says file must provide write(string).

A binary-mode file does not meet that text-stream requirement. Use a text mode stream, or its binary write method instead. Set flush=True to push buffered text to the stream at once, such as a progress message.

Conclusion

Use print() to write readable text, with sep and end for layout and file for choosing a text destination. Keep that output separate from a function’s return value.

For exact parameter rules, consult the Python built-in functions reference. The Python input and output tutorial covers formatted text and files.

FAQ

These answers address common questions about calling print() and displaying function results.

Is print() a function in Python?

Yes. print() is a built-in function that writes text to a stream, usually sys.stdout.

How do I print a function’s result?

Call the function inside print(), such as print(calculate()). The function’s returned value is displayed, while print() itself returns None.

Can print() write to a file?

Yes. Open a text file and pass its file object with file=, such as print(‘Run complete’, file=report).

Safa Mulani
Safa Mulani

An enthusiast in every forthcoming wonders!

Articles: 189