Nearby lessons
159 of 159Python - Output Statements
- Understand the different forms of the print() function
- Use escape characters such as \n and \t to format output
- Combine and repeat strings with the + and * operators
- Control separators and line endings with the sep and end attributes
- Format output using % specifiers and the format() method
Introduction
The print() function is used to display output on the screen.
It is one of the most commonly used functions in Python.
Form 1: print() Without Any Argument
If print() is called without any argument, it prints a blank line.
Example - print()
Form 2: print(String)
You can pass a string to the print() function to display it.
Example - Print String
Using Escape Characters
Escape characters are used to format the output.
Example - New Line (\n)
Example - Tab (\t)
Using Repetition Operator (*)
The * operator repeats a string multiple times.
Example - Number * String
Example - String * Number
Using + Operator
If both operands are strings, the + operator joins them together.
Example - String Concatenation
Important Note
- If both operands are strings,
+joins them. - If both operands are numbers,
+performs addition. - If one operand is a string and the other is a number, Python raises an error.
Difference Between "+" and ","
Explanation
The + operator joins strings without adding spaces.
The comma (,) automatically inserts a space between values.
Form 3: print() with Multiple Arguments
The print() function can display multiple values at the same time.
Example - Multiple Arguments
Important Note
By default, print() separates multiple values with a space.
Using sep Attribute
The sep attribute changes the separator between values.
Example - sep Attribute
Form 4: print() with end Attribute
The end attribute changes what is printed after each print() statement.
Example - Default end
Example - Custom end
Important Note
The default value of the end attribute is '\n', which prints a new line.
Form 5: print(object)
You can print any Python object directly.
Example - Printing Objects
Form 6: print(String, Variables)
You can print a string together with variables.
Example - String with Variables
Form 7: Formatted String Using % Operator
Python supports formatted output using format specifiers.
| Specifier | Data Type |
|---|---|
%i | Integer |
%d | Integer |
%f | Float |
%s | String |
Example - Formatted String
Example - String and List
Form 8: print() Using format() Method
The format() method replaces placeholders using the given values.
Example - format()
Real World Usage
The print() function is commonly used in:
- Displaying results
- Debugging programs
- Showing reports
- User interaction
- Console applications
Important Notes
print()displays output on the screen.+joins two strings.*repeats a string.- By default,
print()separates values with a space. - The
sepattribute changes the separator. - The
endattribute changes the ending character. - You can print objects such as lists and tuples directly.
- Formatted strings support
%i,%d,%f, and%s. - The
format()method provides another way to create formatted output.
Quick Summary
| Feature | Description |
|---|---|
print() | Prints a blank line or output. |
print(String) | Prints a string. |
print(object) | Prints any Python object. |
sep | Changes the separator between values. |
end | Changes the ending character. |
% Formatting | Creates formatted output. |
format() | Formats strings using placeholders. |
- print() is used to display output on the screen
- Escape characters like \n and \t control how output is formatted
- The + operator joins strings and the * operator repeats them
- The sep attribute changes the separator and the end attribute changes the ending character
- The % operator and the format() method create formatted output