simple string formatting question

Gary Herron gherron at islandtraining.com
Fri Dec 14 10:48:30 EST 2007


Neal Becker wrote:
> 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'
>
> Just print sys.argv will almost work, but it's comma-delimited.
>
> There must be some clever way to do this.  Any ideas?
>
>   

Use repr to get the quotes around each output arg, and use join on a
space to the the space delimited string.


>>> theList = ['./whatever', 'a', 'b', 'c']
>>> print ' '.join([repr(i) for i in theList])
'./whatever' 'a' 'b' 'c'


Gary Herron




More information about the Python-list mailing list