Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

def show_table(table): Given table as a list of lists of strings, create and ret

ID: 3731410 • Letter: D

Question

def show_table(table): Given table as a list of lists of strings, create and return a formatted string representing the 2D table. Follow these requirements (examples below):

o Each row of the 2D table is on a separate line; the last row is followed by an empty line

o Each column is aligned to the left;

o Columns are separated with a single vertical bar '|'; and there must be exactly one space before and after the vertical bar;

o Every row starts and ends with a vertical bar; and there must be exactly one space after the leading bar and before the ending bar

o Restriction: you must use string formatting, either % or .format().

o Hint: Find out the max width for every column before you start creating the string.

o Assumption: every row of the 2D table has the same number of columns. The table has at least one row and one column.

o Example session: >>> show_table([['A','BB'],['C','DD']])

'| A | BB | | C | DD | '

>>> print(show_table([['A','BB'],['C','DD']]))

| A | BB |

| C | DD |

>>> show_table([['A','BBB','C'],['1','22','3333']])

'| A | BBB | C | | 1 | 22 | 3333 | '

>>> print(show_table([['A','BBB','C'],['1','22','3333']]))

| A | BBB | C |

| 1 | 22 | 3333 |

i need Each column is aligned to the left;

python3 language please

Explanation / Answer

''' I used vary simple formatting. I used .format on each entry Just Iterate over each row and column entry. for each column use current entry like '| {} '.format(row[col]) Append this to already existing string when you finish with a row append | and at last Sample Input: [['A','BB'],['C','DD']] Sample Output: | A | BB | | C | DD | ''' #Function to format string def show_table(table): #Declaring empty string to store formatted string new_string = '' #Loop over rows , taking a row at a time for row in table: #Iterating over index of row string list for col in range(len(row)): line = row[col] #adding new value by adding one space before and after it. new_string += '| {} '.format(row[col]) #Adding last bar and nee line new_string += '| ' #String new string return new_string #Calling above function and displaying output print(show_table([['A','BB'],['C','DD']]))