вывести в консоли отформатированную таблицу in python

main.py
# Define the data for the table
data = [
    ["Name", "Age", "City"],
    ["Alice", 25, "New York"],
    ["Bob", 30, "Los Angeles"],
    ["Charlie", 20, "Chicago"]
]

# Define the maximum width for each column
column_widths = [max(len(str(item)) for item in column) for column in zip(*data)]

# Print the table
for row in data:
    print("|".join(str(item).ljust(width) for item, width in zip(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.

gistlibby LogSnag