• Home
  • 2.1 Printing Output

2.1 Printing Output

View Categories

2.1 Printing Output

Printing output is one of the first things every Python programmer learns. Almost every Python program displays some kind of information, such as a welcome message, the result of a calculation, or the value stored in a variable.

Python provides a built-in function called print() to display information on the console.

The print() function can display:

  • Text (strings)
  • Numbers
  • Variables
  • Expressions
  • Multiple values
  • Python objects such as lists, tuples, and dictionaries

Syntax #

print(*objects, sep=' ', end='\n')

Parameters #

Parameter Description
*objects One or more values to display.
sep Separator inserted between multiple values. The default is a single space (' ').
end Character(s) printed after the output. The default is a newline ('\n').

Example 1: Printing a String #

main.py

print("Hello World")

Output #

Hello World

Explanation #

  • print() is a built-in Python function used to display output on the console.
  • "Hello World" is a string literal enclosed in double quotes.
  • Python prints the string exactly as it appears.
  • After printing the text, print() automatically moves the cursor to the next line.

Example 2: Printing Different Data Types #

main.py

print(100)
print(99.95)
print(True)

Output #

100
99.95
True

Explanation #

  • 100 is an integer.
  • 99.95 is a floating-point number.
  • True is a Boolean value.
  • The print() function can display different data types without requiring any additional conversion.

Example 3: Printing Variables #

main.py

name = "Alice"
age = 22

print(name)
print(age)

Output #

Alice
22

Explanation #

  • The variable name stores the string "Alice".
  • The variable age stores the integer 22.
  • Passing a variable to print() displays the value stored in the variable.
  • Variable names should not be enclosed in quotes. Otherwise, Python prints the text instead of the variable’s value.

Example 4: Printing Text and Variables Together #

main.py

name = "Alice"
marks = 92

print("Student:", name)
print("Marks:", marks)

Output #

Student: Alice
Marks: 92

Explanation #

  • The first argument is a string literal.
  • The second argument is a variable.
  • Multiple values are separated using commas.
  • By default, Python inserts a single space between the printed values.

Example 5: Printing Multiple Values #

main.py

print("Python", "Java", "C++")

Output #

Python Java C++

Explanation #

  • Three strings are passed to the print() function.
  • Python prints them in the same order.
  • Since no separator is specified, a single space is automatically inserted between them.

Example 6: Using the sep Parameter #

main.py

print("Python", "Java", "C++", sep=" | ")

Output #

Python | Java | C++

Explanation #

  • sep stands for separator.
  • It specifies what should be printed between multiple values.
  • In this example, " | " is used instead of the default space.

Example 7: Using the end Parameter #

main.py

print("Hello", end=" ")
print("World")

Output #

Hello World

Explanation #

  • By default, every call to print() ends with a newline (\n).
  • The end parameter changes what is printed after the output.
  • Here, end=" " prints a space instead of moving to the next line.
  • As a result, the second print() continues on the same line.

Example 8: Printing the Result of an Expression #

main.py

a = 10
b = 20

print(a + b)
print(a * b)

Output #

30
200

Explanation #

  • a + b is an arithmetic expression.
  • a * b is another arithmetic expression.
  • Python evaluates each expression before passing the result to print().
  • Only the final result of the expression is displayed.

Example 9: Printing Python Collections #

main.py

print([10, 20, 30])
print((1, 2, 3))
print({"name": "Alice", "age": 22})

Output #

[10, 20, 30]
(1, 2, 3)
{'name': 'Alice', 'age': 22}

Explanation #

  • print() can display Python objects directly.
  • The first statement prints a list.
  • The second statement prints a tuple.
  • The third statement prints a dictionary.
  • Python automatically converts these objects into a readable format before displaying them.

Common Mistakes #

1. Forgetting Quotes Around a String #

Incorrect

print(Hello)

Output #

NameError: name 'Hello' is not defined

Reason

Python assumes Hello is a variable. Since no such variable exists, a NameError is raised.

Correct

print("Hello")

2. Forgetting Parentheses #

Incorrect

print "Hello"

Output #

SyntaxError: Missing parentheses in call to 'print'

Reason

In Python 3, print is a function and must always be called using parentheses.

Correct

print("Hello")

3. Printing the Variable Name Instead of Its Value #

Incorrect

name = "Alice"

print("name")

Output #

name

Reason

The text "name" is a string literal, so Python prints it exactly as written.

Correct

name = "Alice"

print(name)

4. Confusing sep and end #

Incorrect Expectation

print("A", "B", end="-")

Output #

A B-

Reason

The end parameter controls what is printed after the output, not between the values.

Correct

print("A", "B", sep="-")

Output #

A-B

Key Points to Remember #

  • print() is the built-in function used to display output on the console.
  • It can print strings, numbers, variables, expressions, and Python objects.
  • Multiple values can be printed in a single print() statement.
  • By default, values are separated by a single space.
  • The sep parameter changes the separator between multiple values.
  • The end parameter changes what is printed after the output.
  • Strings must always be enclosed in quotes.
  • Variables should be written without quotes.
  • print() automatically converts Python objects into a readable format before displaying them.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *