simple string formatting question

Bruno Desthuilliers bruno.42.desthuilliers at wtf.websiteburo.oops.com
Fri Dec 14 10:44:56 EST 2007


Neal Becker a écrit :
> I have a list of strings (sys.argv actually).  I want to print them as a
> space-delimited string (actually, the same way they went into the command
> line, so I can cut and paste)
> 
> So if I run my program like:
> ./my_prog a b c d
> 
> I want it to print:
> 
> './my_prog' 'a' 'b' 'c' 'd'

This should do what you want:

   print " ".join("'%s'" % arg for arg in sys.argv)

NB : if your version of Python predates generator expressions, use a 
list comp instead:

   print " ".join(["'%s'" % arg for arg in sys.argv])



More information about the Python-list mailing list