Python - Console Input/Output

9.
How do you redirect the output of the print() function to a file?

In Python, you can redirect the output of the print() function to a file by using the file parameter. The file parameter allows you to specify a file object to which the output will be written.

Here's an example to demonstrate how to redirect the output to a file:


# Redirecting print output to a file
output_file = open("output.txt", "w")

# Using the print() function with the file parameter
print("Hello, File!", file=output_file)

# Closing the file
output_file.close()

In this example, the open() function is used to create a file object named output_file in write mode ("w"). The print() function is then used with the file parameter to redirect the output to the specified file. Finally, the file is closed using the close() method.

The content of the "output.txt" file after running the program would be:

Hello, File!

10.
What is the sep parameter in the print() function used for?

The sep parameter in the print() function is used to specify the separator between multiple items that are printed. By default, the separator is a space character, but you can customize it using the sep parameter.

Here's an example to illustrate the use of the sep parameter:

# Using the sep parameter to customize the separator
name = "John"
age = 25

# Default separator (space)
print("Name:", name, "Age:", age)

# Custom separator (comma and space)
print("Name:", name, "Age:", age, sep=", ")

In this example, the first print() statement uses the default separator (space) between the items. The second print() statement uses a custom separator (", ") specified by the sep parameter.

Here's how the program works:

Name: John Age: 25
Name: John, Age: 25

11.
Explain the concept of string interpolation in Python.

String interpolation in Python refers to the process of embedding expressions or variables within a string to create a formatted output. There are several ways to achieve string interpolation in Python, including the use of f-strings, the format() method, and the older % formatting method.

Here's an example using f-strings for string interpolation:

# String interpolation using f-strings
name = "Alice"
age = 30

# Using f-string to embed variables in the string
message = f"Hello, my name is {name} and I am {age} years old."

# Displaying the interpolated string
print(message)

In this example, the f-string f"Hello, my name is {name} and I am {age} years old." is used to embed the variables name and age directly within the string.

Here's how the program works:

Hello, my name is Alice and I am 30 years old.

12.
How can you clear the console screen in Python?

In Python, you can clear the console screen by using the os module along with system-specific commands. The os.system() function can be used to execute a command that clears the console screen.

Here's an example program to demonstrate how to clear the console screen:

# Clearing the console screen in Python

# Importing the os module
import os

# Platform-specific command to clear the console screen
clear_command = "cls" if os.name == "nt" else "clear"

# Using os.system() to execute the clear command
os.system(clear_command)

# Displaying a message after clearing the screen
print("Console screen cleared!")

In this example, the os.name is used to check the operating system. If it is Windows (nt), the clear command is set to "cls"; otherwise, for Unix-based systems, it is set to "clear". The os.system() function is then used to execute the clear command.

Note: The effectiveness of clearing the console screen may depend on the specific terminal or console being used.

The output of this program does not need to be displayed here, as it involves clearing the console screen.