-
1. Introduction to Python
-
2. Python Basics
-
3. Working with Data Structures
-
4. Functions and Modules
-
5. Object-Oriented Programming (OOP)
-
6. File Handling
-
7. Error and Exception Handling
-
8. Python for Data Analysis
-
9. Advanced Topics in Python
-
10. Working with APIs
-
11. Python for Automation
-
12. Capstone Projects
- 13. Final Assessment and Quizzes
2.2.2 Printing output with print()
The print() function is one of the most commonly used functions in Python for displaying output to the console or terminal. It allows you to show information, results, or any other message to the user.
Basic Syntax of print()
print(object, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
- object: The value or expression you want to print. You can pass one or more objects.
- sep: (Optional) A string inserted between the objects. The default is a space (' ').
- end: (Optional) A string appended after the last object. By default, it's a newline ('\n'), which moves the cursor to the next line after printing.
- file: (Optional) The output stream. By default, it writes to sys.stdout (i.e., the console).
- flush: (Optional) If True, it forces the output to be written immediately.
1. Basic Usage of print()
print("Hello, World!")
This will display the string "Hello, World!" in the console. The default behavior is that the output is followed by a newline, meaning the cursor will move to the next line.
2. Printing Multiple Objects
You can pass multiple objects (strings, numbers, variables, etc.) to the print() function, and it will print them with a space between them by default.
name = "Alice" age = 30 print("Name:", name, "Age:", age)
Output:
Name: Alice Age: 30
- The print() function automatically separates the objects with a space by default.
3. Using the sep Argument
You can customize how the objects are separated using the sep argument.
print("apple", "banana", "cherry", sep=", ")
Output:
apple, banana, cherry
- In this case, the sep argument is set to ", ", so the objects are separated by a comma and a space.
4. Using the end Argument
By default, print() ends its output with a newline (\n). You can change this by using the end argument.
print("Hello", end=" ") print("World!")
Output:
Hello World!
- The first print() statement ends with a space instead of a newline because we set end=" ".
- The second print() statement continues on the same line, printing "World!".
5. Printing Variables
You can print the values of variables directly:
x = 10 y = 20 print(x, y)
Output:
10 20
- The print() function prints the values of x and y with a space in between by default.
6. String Concatenation in print()
You can concatenate strings and other data types using the + operator:
name = "Bob" greeting = "Hello, " + name + "!" print(greeting)
Output:
Hello, Bob!
- The + operator is used to concatenate the string literals and the name variable to form the complete greeting.
7. Using f-strings for String Interpolation (Python 3.6+)
Python 3.6 introduced f-strings, which allow for more readable and concise string formatting. You can embed expressions inside string literals using curly braces {}.
name = "Charlie" age = 25 print(f"Name: {name}, Age: {age}")
Output:
Name: Charlie, Age: 25
- The f-string f"Name: {name}, Age: {age}" automatically formats the variables inside the string.
8. Printing Special Characters
You can include special characters such as tabs (\t), newlines (\n), and others in the string that you're printing.
print("First Line\nSecond Line\nThird Line")
Output:
First Line Second Line Third Line
- \n creates a new line after each string.
print("Item1\tItem2\tItem3")
Output:
Item1 Item2 Item3
- \t inserts a tab space between the items.
9. Printing to a File
You can redirect the output of print() to a file using the file argument.
with open("output.txt", "w") as file: print("Hello, World!", file=file)
- This will write "Hello, World!" to a file named output.txt instead of printing it to the console.
10. Printing Without a Newline
If you don't want print() to add a newline after each output, set the end argument to an empty string ("").
print("Hello", end="") print("World!")
Output:
HelloWorld!
- The two print statements print the strings without moving to a new line after each.
Summary:
- print() is used to display output to the console in Python.
- By default, print() adds a newline at the end of its output.
- You can customize the separator between printed objects using the sep argument.
- The end argument allows you to control what is printed at the end of the output (e.g., newline, space, etc.).
- f-strings provide a cleaner way to embed expressions directly in strings (from Python 3.6+).
- Special characters such as \n (new line) and \t (tab) can be used within strings for formatting.
Commenting is not enabled on this course.