Reputation: 12452
From the docs, raw_input() reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
with that note,
a = 'testing: '
sep = '-'
c = raw_input('give me some args: ') <--- giving 'a b c d'
def wrap( a, sep, *c):
print a + sep.join(c)
wrap(a, sep, c)
str = 'a b c d'
print sep.join(str)
they should both print out the same thing but...
print a + sep.join(c)
gives testing: a b c d
print sep.join(str)
gives a- -b- -c- -d
why doesn't sep.join()
works inside wrap function?
EDIT changing from *c to c makes the output the same but this somewhat confuses me because i thought *c unpacks the args but when i print c, it give ms ('a b c d',) compared to a string of 'a b c d' so in a sense, it is combining them to a single tuple entity which is the opposite of unpacking?
or... it does not unpack string but only lists?
Upvotes: 1
Views: 607
Reputation: 24304
this:
>>> wrap(a, sep, str)
testing: a b c d
is different from:
>>> wrap(a, sep, *str)
testing: a- -b- -c- -d
A string is a list of characters. You can change the function signature from def wrap(a, sep, *c):
to def wrap(a, sep, c):
and you should be fine.
Upvotes: 1
Reputation: 28360
join expects an array not a string so if you are trying to use the arguments as separate items you need to use c = c.spilt(' ')
and get rid of the * in the def wrap
.
To my surprise sep.join(str)
is treating str as an array of chars hence the extra -s between letters and spaces.
Upvotes: 2
Reputation: 1461
In your function c
is a tuple of one element because of the splat (*), therefore the separator is never used.
In the main program, you're calling join on a string, which is split into its characters. Then you get as many istances of the separator as many charactes are there (minus one).
Upvotes: 3