Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
In this tutorial, we'll learn about the print()
function in Python, which is a built-in function used to display output to the console. The print()
function allows you to output text, numbers, and other data types in a human-readable format.
Example 1: Basic usage of the print()
function
print("Hello, world!")
This will output the text "Hello, world!" to the console.
Example 2: Printing multiple values
You can pass multiple arguments to the print()
function, separated by commas. The values will be printed in the order they are provided, separated by a space.
name = "John" age = 25 print("My name is", name, "and I am", age, "years old.")
This will output: "My name is John and I am 25 years old."
Example 3: Using a custom separator
By default, the print()
function separates the arguments with a space. You can change the separator using the sep
parameter.
print("apple", "banana", "cherry", sep=", ")
This will output: "apple, banana, cherry"
Example 4: Printing on a new line
By default, the print()
function adds a newline character (\n
) at the end of the output, which moves the cursor to the next line. You can change the end character using the end
parameter.
print("Hello, world!", end=" ") print("Welcome to Python.")
This will output: "Hello, world! Welcome to Python."
Example 5: Using formatted strings
You can use formatted strings (f-strings) to create more complex output by embedding expressions inside string literals, using curly braces {}
.
name = "John" age = 25 print(f"My name is {name} and I am {age} years old.")
This will output: "My name is John and I am 25 years old."
Example 6: Printing a list or other iterable
You can use the print()
function to display the contents of a list, tuple, or other iterable.
fruits = ["apple", "banana", "cherry"] print(fruits)
This will output: ['apple', 'banana', 'cherry']
In summary, the print()
function is a built-in Python function that allows you to display output to the console in a human-readable format. You can print multiple values, use custom separators and end characters, and format your output using formatted strings (f-strings) or other formatting techniques. The print()
function is essential for providing feedback and displaying the results of your code.
How to Use print()
to Display Values in Python:
print()
function is used to display values in Python.# Example message = "Hello, World!" print(message)
Printing to the Screen with print()
in Python:
print()
is to output information to the console.# Example print("This is a message.")
Redirecting Output to a File with print()
in Python:
print()
to a file using the file
parameter.# Example with open("output.txt", "w") as file: print("Output to a file", file=file)
Formatting Output with the print()
Function in Python:
# Example name = "Alice" age = 30 print("Name:", name, "Age:", age)
Print to a File Instead of the Console in Python:
print()
output to a file instead of the console.# Example with open("output.txt", "w") as file: print("Print to a file", file=file)
Customizing print()
Output in Python:
print()
output by adjusting the separator and end characters.# Example print("Custom", "output", sep="-", end="!\n")
Logging with print()
in Python:
print()
for basic logging purposes.# Example log_message = "Log this message" print("LOG:", log_message)
Print to a Specific File in Python:
print()
output to a specific file using the file
parameter.# Example with open("log.txt", "a") as log_file: print("Log this message", file=log_file)
Advanced Usage of print()
for Debugging in Python:
print()
for debugging by including variable values in the output.# Example variable = 42 print("Debug: Variable =", variable)