Melanie432k Melanie432k - 1 year ago 62
Python Question

I was just wondering how can I print a list in python using def? More info on description

So I have a pprogram, and everything is ok except that the lists should be printed without the square brackets included.
so my program asks people what shape they are interested in, then asks them what lengths they want each side of the shapes, then it calculates the volume of each of the three shapes. I have a def function for lengths of different shapes, which inputs them into a list, so for example:

This is an example of what it looks like:

I have them in a list like

cubeVolumes = []


def calcCubeVolumes(length):
volume1 = int(length) ** 3
return volume1

and then it'll do that for the other two shapes too, so pyramid and ellipsoid, and then at the end it will print them into a list like:

"Your volumes for cube are: [ 54, 32, 12] etc. (print("Cube:", cubeVolumes)) to be exact

I really hope this makes sense, but my question is, how do I print each of the lists without the brackets? I need help printing a def function that converts each element to a string and adds commas in between inside a loop, except for the last item in the list.

Answer Source
print(*cubeVolumes, sep=', ')

This is the same as passing the elements of the list into print as arguments. sep is the seperator character

print(54, 32, 12, sep=', ')

To do it with some other text:

print("Cube Volumes: ", end='') #No line break after this print
print(*cubeVolumes, sep=', ')


Cube Volumes: 54, 32, 12
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download