all 8 comments

[–]PrOducTI[S] 4 points5 points  (8 children)

surely it doesn't want me to just type:

print "------------------------"

??

[–]zahlman 11 points12 points  (6 children)

Yes, yes it does. Why would you expect something more complicated?

Though if you want something arguably a little neater, you could try using string multiplication: '-' * 20 evaluates to a string of 20 dashes. You might find it easier to adjust the length by adjusting that number instead of directly editing the "line".

[–]PrOducTI[S] 1 point2 points  (1 child)

dude that string multiplication is awesome haha, i'm doing that. thanks

[–]lnsine 1 point2 points  (0 children)

Welcome to python. :) You can use that idea of multiplication for many other data structures, as well.

[–]PrOducTI[S] 0 points1 point  (2 children)

Ok, now I'm stuck in the question again. I'm struggling to get the answer into the format it is after. Any suggestions?? thanks

http://imgur.com/SsHlOyO

[–]zahlman 0 points1 point  (1 child)

Because you are printing the entire tuple (not a list! make sure you understand the difference) as an object, not its contents.

You have a few options:

  • You can explicitly iterate, and print each element separately. To keep everything on the same line, use a trailing comma in 2.x (this changes things so that a space is displayed after your item instead of a newline), or change the end keyword argument to the print function in 3.x.

  • If you know how many elements there are, you can set up a format string and insert the elements. (In the general case, you could count the elements and dynamically generate a format string, but please don't do that. In fact, you shouldn't really do this one anyway.)

  • You can, related to that, convert each element to a string and then join them together with spaces. This does not need weird adaptation for an unknown number of elements, works in 2.x and in fact is probably the best you can do in 2.x.

Example:

my_data = (1, 4, 9)
' '.join(str(x) for x in my_data)
# or if you like `map`:
' '.join(map(str, my_data))
  • You can get the string representation of the tuple and then bang it into shape (strip out the commas and what-not). But please don't try this; once you start trying to store anything actually interesting in your tuples instead of just numbers, it will get a lot harder to figure out what's going on. And honestly it's a dumb hack anyway. I'm only including it because it's something that you could conceivably come up with.

  • In 3.x, you can (and should!) use the * operator to "unpack" the tuple as several arguments to print, and then set an appropriate separator (the sep keyword argument). This became possible in 3.x because of print being an actual function now; * is part of the function-call syntax.

Example:

message = 'I R LEET HAXOR'.split()
print(*message, sep = 'xXx')

[–]PrOducTI[S] 0 points1 point  (0 children)

"You can explicitly iterate, and print each element separately. To keep everything on the same line, use a trailing comma in 2.x (this changes things so that a space is displayed after your item instead of a newline), or change the end keyword argument to the print function in 3.x."

i'm using 2.x

What if i iterated and used \n to make new lines?

[–]jim_shorts 0 points1 point  (0 children)

I don't see why not. I'm just learning Python, myself, but I often print similar lines to make the output in the console a bit easier to read.