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: abcd
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 ('abc d',) compared to a string of 'abcd' 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?
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).
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.