Learn how to handle Python errors and exceptions effectively. Fix common Python errors like SyntaxError, TypeError, and NameError with practical examples.
print
Functionprint
function.print
function features like formatting and special characters.print
The print
function is used to output text or variables to the console. or to a file.
Video: Use of print() function in python
Syntax:
print(value1, value2, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
Parameters:
value1, value2, ...
: The values to be printed. Multiple values can be separated by commas.sep
: (Optional) Specifies how to separate multiple values. Default is a space ' '
.end
: (Optional) Specifies what to print at the end. Default is a newline character '\n'
.file
: (Optional) Specifies the file where to print. Default is sys.stdout
(console).flush
: (Optional) Specifies whether to forcibly flush the stream. Default is False
.Instructions:
Examples
# Task 1.1: Print a simple message
print("Hello, world!")
# Task 1.2: Print multiple items
print("Hello", "world", 2024)
Instructions
Examples
# Task 2.1: Print different data types
print(42)
print(3.14159)
print("This is a string")
sep
and end
ParametersInstructions
Examples
# Task 3.1: Change the separator
print("apple", "banana", "cherry", sep=", ")
# Task 3.2: Change the ending character
print("Hello", end=" ")
print("world!")
# Task 3.3: Print with a custom ending character:
print("Hello", "World", end="!")
Instructions
# Task 4.1: print a integer variable
x = 5
print(x)
# Task 4.2: print a string variable
message = 'Python is fun'
# print the string message
print(message)
# Task 4.3: print a string variable
message = "How are you?"
print(message)
Instructions
Examples
# Task 6.1: Print a newline character
print("Hello\nWorld")
# Task 6.2: Print a tab character
print("Hello\tWorld")
video: Python line break: How to Print Line Break
# Task 6.3: Print a tab character
print("Name\tAge\tCity")
print("Alice\t30\tNew York")
print("Bob\t25\tLos Angeles")
Output:
Name Age City
Alice 30 New York
Bob 25 Los Angeles
In this example, \t
is used to align the columns of text.
In Python, the \t
character is a special escape sequence that represents a horizontal tab. When used in the print
function or any other string operation, it inserts a tab space in the output. This can be particularly useful for formatting text to make it more readable.
\t
can be combined with other string manipulation techniques, such as f-strings
# Task 6.4: Print a tab character with f-strings
name = "Alice"
age = 30
city = "New York"
print(f"{name}\t{age}\t{city}")
Instructions
Examples
# Task 7.1: Print to a file
with open("output.txt", "w") as file:
print("Hello, file!", file=file)
When you use the instruction with open(“output.txt”, “w”) as file in Python, the file is created in the current working directory of your program. On an Android system, this could be different depending on the environment where the code is executed (e.g., a specific app’s data directory, a shared storage location, etc.).
To determine the exact path, you can use the os module to get the current working directory:
import os
print(os.getcwd())
Regarding file closure, when you use the with
statement to open a file, Python automatically takes care of closing the file for you once the block of code under the with
statement is executed. There is no need to explicitly close the file; it is done automatically when the block is exited. This is one of the benefits of using the with statement for file operations. for more details, see Understanding Python’s os.getcwd(): Get Current Working Directory with Examples
Objective: Learn about syntax errors in Python by examining and correcting a sample code snippet.
Instructions:
Code Snippet:
print("Hello World!"
Steps:
print("Hello World!")
Syntax error:
See also:
print()
function to display it.\t
(tab character) to align item names and prices neatly.name
, age
, and hobby
.print()
function to create a sentence incorporating these variables."Hello World\n" * 100
) as a possible solution.related video: 100 times “hello world” without loop
print()
statements.\n
).Related Video: How to print multiple lines
"w"
).write()
method.