Drawing a table for data from file I wrote a function for drawing a table for data that comes from file:

import data_manager
import os

table = data_manager.get_table_from_file('games.csv')

len_of_longest_name = 0
i = 0
while i &lt; len(table):
    if len_of_longest_name &lt; len(table[i][1]):
        len_of_longest_name = len(table[i][1])
    i += 1

i = 0
len_of_longest_studio = 0
while i &lt; len(table):
    if len_of_longest_studio &lt; len(table[i][2]):
        len_of_longest_studio = len(table[i][2])
    i += 1
print("/", 6 * "-", "|", (len_of_longest_name - 2) * "-", "|", (len_of_longest_studio - 2) * "-", "|", "---", "\\")

# constructing string

i = 0
while i &lt; len(table):
    string = '|'
    string += table[i][0] + '|'
    if len(table[i][1]) &lt; len_of_longest_name:
        spaces = (len_of_longest_name - len(table[i][1])) * ' '
        string += table[i][1] + spaces + '|'
    else:
        string += table[i][1] + '|'
    if len(table[i][2]) &lt; len_of_longest_studio:
        spaces = (len_of_longest_studio - len(table[i][2])) * ' '
        string += table[i][2] + spaces + '|'
    else:
        string += table[i][2] + '|'
    string += table[i][3] + '|'
    if len(table[i][4]) &lt; 2:
        string += table[i][4] + ' |'
    else:
        string += table[i][4] + '|'
    if i &lt; len(table) - 1:
        print(string)
        print("|", 6 * "-", "|", (len_of_longest_name - 2) * "-", "|", (len_of_longest_studio - 2) * "-", "|", "---", "|")
    else:
        print(string)
    i += 1
print("\\", 6 * "-", "|", (len_of_longest_name - 2) * "-", "|", (len_of_longest_studio - 2) * "-", "|", "---", "/")


The code works as I want but I think there's a better (shorter) method to do the same. Could you show me the method?

The output looks like this:
Python has many great ways to iterate.  In general if you find yourself looping on an integer, like in the following code, there is a good chance you are doing it wrong.  It is generally better to iterate on the data structure itself.

Sample code:

I will use this loop as an example:

len_of_longest_name = 0
i = 0
while i &lt; len(table):
    if len_of_longest_name &lt; len(table[i][1]):
        len_of_longest_name = len(table[i][1])
    i += 1


Don't iterate on the length of a data structure:

This is probably not what you want:

while i &lt; len(table):


It would likely be better as something like:

for row in table:


Or, if for some reason you really need the index, then:

for i, row in enumerate(table):


Example with looping on length removed:

So removing i, and looping on table, we get:

len_of_longest_name = 0
for row in table:
    if len_of_longest_name &lt; len(row[1]):
        len_of_longest_name = len(row[1])


Use builtin functions.

Python have a max() function.  Using max(), the example becomes:

len_of_longest_name = 0
for row in table:
    len_of_longest_name = max(len_of_longest_name, len(row[1]))


Comprehensions:

Since max() can take a comprehension the example loop can be reduced to:

len_of_longest_name = max(len(row[1]) for row in table)


That is six lines of code reduced to one.