I couldn't put my question into words accurately, so I am sorry if this question has been answered. I will try to explain it in code.
String_0 = "Car"
String_1 = "Bus"
String_2 = "Plane"
for i in range(3):
print(String_i)
I want the output like this:
Car
Bus
Plane
Edit: My original code is this:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
def tablePrinter(tableData):
Space_0 = 0
Space_1 = 0
Space_2 = 0
for i in range(len(tableData[0])):
if len(tableData[0][i]) > Space_0:
Space_0 = len(tableData[0][i])
for i in range(len(tableData[1])):
if len(tableData[1][i]) > Space_1:
Space_1 = len(tableData[1][i])
for i in range(len(tableData[0])):
if len(tableData[2][i]) > Space_2:
Space_2 = len(tableData[2][i])
for i in range(len(tableData[0])):
print(tableData[0][i].rjust(Space_0), tableData[1][i].rjust(Space_1),
tableData[2][i].rjust(Space_2), end = "")
print()
tablePrinter(tableData)
My output is this:
apples Alice dogs
oranges Bob cats
cherries Carol moose
banana David goose
This output is what I wanted. But I want to simplify code by editing this part:
for i in range(len(tableData[0])):
print(tableData[0][i].rjust(Space_0), tableData[1][i].rjust(Space_1),
tableData[2][i].rjust(Space_2), end = "")
print()
I want a structure like this:
for i in range(len(tableData[0])):
for k in range(len(tableData)):
print(tableData[k][i].rjust(Space_k), end = " ")
print()
I tried to apply your advices to this part. I tried to add globals(), locals() in front of tableData[k][i].rjust(Space_k)
, or in front of Space_k
; tried {} .format structure, tried to use list, but I couldn't solve.
Edit2: So I managed to solve the problem with using lists. Thanks to everyone.
spaceList = [Space_0, Space_1, Space_2] # I added this to function.
Instead of this line in last part,
print(tableData[k][i].rjust(Space_k), end = " ")
I used this:
print(tableData[k][i].rjust(spaceList[k]), end = " ")