Python - Console Input/Output

17.
How can you handle special characters or escape sequences in user input?

To handle special characters or escape sequences in user input in Python, you can use the input() function along with appropriate string manipulation or parsing techniques. This allows you to interpret and process special characters entered by the user.

Here's an example program that demonstrates how to handle special characters in user input:

# Handling special characters in user input

# Using input() to get user input
user_input = input("Enter a string with special characters: ")

# Displaying the original input
print("Original Input:", user_input)

# Using repr() to display the raw representation of the string
print("Raw Representation:", repr(user_input))

In this example, the input() function is used to get user input, including special characters. The original input is then displayed using the print() function. Additionally, the repr() function is used to display the raw representation of the string, which includes escape sequences.

Here's how the program works:

Enter a string with special characters: Original Input: Hello\nWorld
Raw Representation: 'Hello\\nWorld'

18.
Discuss the use of the eval() function for processing user input.

The eval() function in Python is used to evaluate a string as a Python expression. It can be employed for processing user input when you want to interpret and execute the user-supplied code dynamically. However, it's essential to use eval() with caution, as it can pose security risks if used with untrusted input.

Here's an example program that demonstrates the use of eval() for processing user input:

# Using eval() for processing user input

# Getting a mathematical expression from the user
user_input = input("Enter a mathematical expression: ")

try:
    # Using eval() to evaluate the expression
    result = eval(user_input)

    # Displaying the result
    print("Result:", result)
except Exception as e:
    # Handling potential errors in user input
    print("Error:", e)

In this example, the input() function is used to get a mathematical expression from the user. The eval() function is then used to evaluate the expression dynamically. The result is displayed, and potential errors in the user input are handled using a try-except block.

Here's how the program works:

Enter a mathematical expression: Result: 25

19.
How do you display formatted tables on the console in Python?

In Python, you can use the prettytable library to display formatted tables on the console. The PrettyTable class from this library allows you to create and print tables with a clean and readable format.

Here's an example program that demonstrates how to display a formatted table on the console using prettytable:

# Displaying formatted tables on the console using prettytable

# Installing the prettytable library
# pip install prettytable

# Importing the PrettyTable class
from prettytable import PrettyTable

# Creating a PrettyTable object
table = PrettyTable()

# Defining table headers
table.field_names = ["Name", "Age", "City"]

# Adding data to the table
table.add_row(["Alice", 28, "New York"])
table.add_row(["Bob", 35, "London"])
table.add_row(["Charlie", 22, "Paris"])

# Displaying the formatted table
print(table)

In this example, the prettytable library is used to create a PrettyTable object. Table headers are defined using the field_names attribute, and data is added to the table using the add_row() method. Finally, the formatted table is displayed on the console using print(table).

Here's how the program works:

+---------+-----+---------+
|  Name   | Age |  City   |
+---------+-----+---------+
| Alice   |  28 | New York|
| Bob     |  35 | London  |
| Charlie |  22 |  Paris  |
+---------+-----+---------+

20.
What is the purpose of the pprint module for pretty printing in Python?

The pprint module in Python stands for "pretty-print" and is used for producing visually more readable representations of data structures, especially nested structures. It is an extension of the built-in pprint module and provides a PrettyPrinter class for customized pretty printing.

Here's an example program that demonstrates the purpose of the pprint module for pretty printing:

# Using the pprint module for pretty printing

# Importing the pprint module
import pprint

# Creating a nested dictionary for demonstration
data = {
    "name": "John",
    "age": 30,
    "city": "New York",
    "contacts": {
        "email": "john@example.com",
        "phone": "123-456-7890"
    }
}

# Using pprint.PrettyPrinter for pretty printing
pretty_printer = pprint.PrettyPrinter(indent=2)
pretty_printer.pprint(data)

In this example, the pprint module is used to create a PrettyPrinter object. The indent parameter is set to 2 to specify the number of spaces for each indentation level. The pprint() method is then called on the PrettyPrinter object to pretty print the nested dictionary.

Here's how the program works:

{
  'age': 30,
  'city': 'New York',
  'contacts': {'email': 'john@example.com', 'phone': '123-456-7890'},
  'name': 'John'
}