Nearby lessons

108 of 108

Python - 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
Python 3 Runtime
Loading Editor...
Output Preview
(Blank Line)

Form 2: print(String)

You can pass a string to the print() function to display it.

Example - Print String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Hello World

Using Escape Characters

Escape characters are used to format the output.

Example - New Line (\n)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Hello
World

Example - Tab (\t)

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
Hello    World

Using Repetition Operator (*)

The * operator repeats a string multiple times.

Example - Number * String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
HelloHelloHelloHelloHelloHelloHelloHelloHelloHello

Example - String * Number

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
HelloHelloHelloHelloHelloHelloHelloHelloHelloHello

Using + Operator

If both operands are strings, the + operator joins them together.

Example - String Concatenation

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
Hello
Durga
Soft

Example - Custom end

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
[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
Python 3 Runtime
Loading Editor...
Output Preview
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
%iInteger
%dInteger
%fFloat
%sString

Example - Formatted String

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
a value is 10
b value is 20 and c value is 30

Example - String and List

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
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 sep attribute changes the separator.
  • The end attribute 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.
sepChanges the separator between values.
endChanges the ending character.
% FormattingCreates formatted output.
format()Formats strings using placeholders.

🧠 Test Your Knowledge

8 Questions

Progress: 0 / 8
Keep Going!Course Complete!