- Syntax
- Example 1: Printing a String
- Example 2: Printing Different Data Types
- Example 3: Printing Variables
- Example 4: Printing Text and Variables Together
- Example 5: Printing Multiple Values
- Example 6: Using the sep Parameter
- Example 7: Using the end Parameter
- Example 8: Printing the Result of an Expression
- Example 9: Printing Python Collections
- Common Mistakes
- Key Points to Remember
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 #
100is an integer.99.95is a floating-point number.Trueis 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
namestores the string"Alice". - The variable
agestores the integer22. - 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 #
sepstands 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
endparameter 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 + bis an arithmetic expression.a * bis 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
sepparameter changes the separator between multiple values. - The
endparameter 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.