Introduction: Python print() Function
When writing a Python program, you often need to display information on the screen. Whether you’re showing messages, displaying variable values, presenting calculation results, or checking how your program is executing, there needs to be a simple way to produce output.
Without an output function, understanding program behavior, verifying results, and debugging code would be much more difficult.
This is where the Python print() Function becomes useful.
What it is: The print() function is a built-in Python function that displays one or more objects on the standard output (console). It automatically converts supplied values into a readable format, making it useful for displaying messages, variable values, and calculation results.
Review a quick example to understand the basics.
Next, discover its practical use cases to learn how it is used in real programs.
Now let’s understand its syntax, parameters, and return value before exploring practical examples and use cases.
💡 Tip: Ready to learn more beyond print()? Visit the Python Built-in Functions Learning Guideto explore every essential built-in function in a structured learning path.
Syntax, Parameters, Return Value and Examples: Python print() Function
The following section explains the syntax, parameters, return value, and a quick example of the Python print() Function.
Syntax
print(*objects, sep=' ', end='\n', file=None, flush=False)
Parameters
| Parameter | Description |
|---|---|
*objects |
One or more values to be displayed in the output. These can be strings, numbers, variables, expressions, or other Python objects. |
sep |
Specifies the separator inserted between multiple objects. The default separator is a single space. |
end |
Specifies what is printed after the output. By default, a newline character is added. |
file |
Specifies the destination where the output should be written. By default, output is sent to the console. |
flush |
If set to True, the output buffer is flushed immediately. The default value is False. |
Return Value
| Return Value | Description |
|---|---|
| None | The print() function displays the specified output but does not return any value. |
Quick Example
The following example prints a simple message on the screen using the print() function.
print("Welcome to DigiEduTech")
# Output:
Welcome to DigiEduTech
The print() function displays the text exactly as provided inside the quotation marks. It is the simplest way to produce output in a Python program.
How the Python print() function works
- The
print()function accepts one or more objects as input. - It automatically converts each object into a readable format before displaying it.
- Multiple objects are separated by a space by default.
- The
sepparameter lets you specify a custom separator. - The
endparameter controls what is printed after the output. - By default, the output is displayed on the console, but it can also be redirected to a file.
- The function returns
None.
Examples: print() Function
The following examples demonstrate how the Python print() Function works in different programming scenarios:
Example 1: Printing Text
print("Hello, World!")
# Output:
Hello, World!
Explanation: The text enclosed within quotation marks is displayed exactly as it appears. This is the most basic use of the print() function.
Example 2: Printing Variables
name = "Alice"
print(name)
# Output:
Alice
Explanation: Instead of printing fixed text, the print() function displays the value stored in the variable.
Example 3: Printing Multiple Values
name = "Alice"
age = 22
course = "Python"
print(name, age, course)
# Output:
Alice 22 Python
Explanation: Multiple values can be passed to the print() function. By default, Python separates them with a single space.
Example 4: Using the sep Parameter
print("Python", "Java", "C++", sep=" | ")
# Output:
Python | Java | C++
Explanation: The sep parameter replaces the default space with a custom separator, making the output more readable.
Example 5: Using the end Parameter
print("Good", end=" ")
print("Morning")
# Output:
Good Morning
Explanation: By default, print() moves to the next line after displaying output. The end parameter changes this behavior by printing the specified value instead of a newline.
Example 6: Formatted Output Using f-Strings
name = "Alice"
marks = 95
print(f"{name} scored {marks} marks.")
# Output:
Alice scored 95 marks.
Explanation: f-strings allow variables and expressions to be embedded directly inside a string, making formatted output cleaner and easier to read.
Use Cases: When to use the print() Function
Below are some common situations where the Python print() Function becomes useful:
- Displaying messages and instructions to users.
- Printing the values of variables and expressions.
- Showing the results of calculations and program execution.
- Debugging programs by checking intermediate values during execution.
- Creating formatted console output using parameters such as
sepandend. - Displaying dynamic information using formatted strings (f-strings).
Key Takeaways: print() Function
Before wrapping up, here are the key points to remember about the Python print() Function:
- The
print()function displays output on the console. - It can print text, numbers, variables, expressions, and multiple objects.
- The
sepparameter specifies a custom separator between multiple values. - The
endparameter controls what appears after the output. - The function returns
None. - It is commonly used for displaying output, debugging code, and testing programs.
In short, the Python print() Function provides a simple and flexible way to display information, making it an essential tool for writing, testing, and understanding Python programs.