0

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 = []

then

def calcCubeVolumes(length):   
    volume1 = int(length) ** 3
    cubeVolumes.append(volume1)
    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.

Nf4r
  • 1,390
  • 1
  • 7
  • 9
Melanie432k
  • 53
  • 1
  • 1
  • 7

3 Answers3

2
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=', ')

Ouput:

Cube Volumes: 54, 32, 12
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • Hi, this is perfect! and simple thank you.. but when I print it out... it has a comma at the from.. for example Cube Volumes: , 43, 65, 32 can I get rid of it? – Melanie432k Oct 26 '16 at 15:02
0

This should do it:

print("Cube:", ', '.join(['{}'.format(c) for c in cubeVolumes]))
Brian
  • 1,988
  • 1
  • 14
  • 29
  • If you want to be fancy, you don't need to actually add the squared brackets. – xZise Oct 26 '16 at 13:55
  • 2
    @xZise turns out passing in a list to `.join` is more efficient than just passing a generator expression - See the discussion in comments here: http://stackoverflow.com/questions/40178364/using-regex-to-remove-digits-from-string/40178983?noredirect=1#comment67630965_40178983 – Bahrom Oct 26 '16 at 15:09
0

How about something like this

myList = [54,32,12]

print(myList[0], end="")

for item in myList[1:]:
    print(",", item, end="")

Note: requires Python v 3.x for the end="" part to work, this forces the print on the same line, instead of a new line

CJC
  • 400
  • 4
  • 15
  • instead of playing with `range` do `for item in myList[1:]` – Patrick Haugh Oct 26 '16 at 13:49
  • Thanks Patrick, was trying to remember that bit! Answer edited – CJC Oct 26 '16 at 13:56
  • 1
    This should work in Python 2.6+ if you use `from __future__ import print_function`. See also the [`__future__`](https://docs.python.org/2/library/__future__.html) package documentation. – xZise Oct 26 '16 at 13:57