вывести в консоли отформатированную таблицу in python
main.py
# Define the data for the tabledata = [
["Name", "Age", "City"],
["Alice", 25, "New York"],
["Bob", 30, "Los Angeles"],
["Charlie", 20, "Chicago"]
]
# Define the maximum width for each columncolumn_widths = [max(len(str(item)) for item in column) for column inzip(*data)]
# Print the tablefor row in data:
print("|".join(str(item).ljust(width) for item, width inzip(row, column_widths)))
# Output:# Name | Age | City # Alice | 25 | New York # Bob | 30 | Los Angeles# Charlie| 20 | Chicago
536 chars
21 lines
This code snippet defines a table structure as a list of rows where each row is a list of cells, and then prints the formatted table in the console output.