1

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?

1
  • It unpacks when used in "active" code. When used in the argument list of a function, it means (roughly, very): this argument is the collection of all the parameters passed to the function which are not assigned to any other argument. Which allows you to pass a variable number of arguments to a function. Commented Jul 15, 2013 at 20:31

3 Answers 3

3

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).

Sign up to request clarification or add additional context in comments.

3 Comments

yes, i suppose i was confused on the fact that it would "unpack" the argument or a string in this case but it does the opposite. so i would actually have to have the raw_input() in a list instead of string in order to have the same effect which is a bit intriguing.
It depends on what you mean by "the same effect"
the same effect = printing out the same thing
2

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.