Nearby lessons

159 of 159

Python - Output Statements

📌 What You Will Learn
  • 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()

Loading editor…

Form 2: print(String)

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

Example - Print String

Loading editor…

Using Escape Characters

Escape characters are used to format the output.

Example - New Line (\n)

Loading editor…

Example - Tab (\t)

Loading editor…

Using Repetition Operator (*)

The * operator repeats a string multiple times.

Example - Number * String

Loading editor…

Example - String * Number

Loading editor…

Using + Operator

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

Example - String Concatenation

Loading editor…

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 ","

Loading editor…

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

Loading editor…

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

Loading editor…

Form 4: print() with end Attribute

The end attribute changes what is printed after each print() statement.

Example - Default end

Loading editor…

Example - Custom end

Loading editor…

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

Loading editor…

Form 6: print(String, Variables)

You can print a string together with variables.

Example - String with Variables

Loading editor…

Form 7: Formatted String Using % Operator

Python supports formatted output using format specifiers.

Specifier Data Type
%iInteger
%dInteger
%fFloat
%sString

Example - Formatted String

Loading editor…

Example - String and List

Loading editor…

Form 8: print() Using format() Method

The format() method replaces placeholders using the given values.

Example - format()

Loading editor…

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.
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.