Nearby lessons
108 of 108Python - Output Statements
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()
main.py
(Blank Line)
Form 2: print(String)
You can pass a string to the print() function to display it.
Example - Print String
main.py
Hello World
Using Escape Characters
Escape characters are used to format the output.
Example - New Line (\n)
main.py
Hello World
Example - Tab (\t)
main.py
Hello World
Using Repetition Operator (*)
The * operator repeats a string multiple times.
Example - Number * String
main.py
HelloHelloHelloHelloHelloHelloHelloHelloHelloHello
Example - String * Number
main.py
HelloHelloHelloHelloHelloHelloHelloHelloHelloHello
Using + Operator
If both operands are strings, the + operator joins them together.
Example - String Concatenation
main.py
HelloWorld
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 ","
main.py
HelloWorld Hello World
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
main.py
The Values are : 10 20 30
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
main.py
10,20,30 10:20:30
Form 4: print() with end Attribute
The end attribute changes what is printed after each print() statement.
Example - Default end
main.py
Hello Durga Soft
Example - Custom end
main.py
Hello Durga Soft
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
main.py
[10, 20, 30, 40] (10, 20, 30, 40)
Form 6: print(String, Variables)
You can print a string together with variables.
Example - String with Variables
main.py
Hello Durga Your Age is 48 You are teaching Java and Python
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
main.py
a value is 10 b value is 20 and c value is 30
Example - String and List
main.py
Hello Durga ... The List of Items are [10, 20, 30, 40]
Form 8: print() Using format() Method
The format() method replaces placeholders using the given values.
Example - format()
main.py
Hello Durga your salary is 10000 and your friend Sunny is waiting Hello Durga your salary is 10000 and your friend Sunny is waiting
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. |