Neat way to get rid of [" "] in sys.argv[n:] returns

Fredrik Lundh fredrik at pythonware.com
Thu Jul 24 23:20:52 CEST 2008


korean_dave wrote:

> python test.py "testparameter"
> 
> I get this output:
> 
> -------------
> ['testparameter']
> ----------------
> 
> Is there a way to neatly, in one call to the parameter, to get rid of
> the [' and '] without bothering to replace() '[ with "" and replace()
> '] with ''?

Since you're using slicing instead of indexing, you're printing a list, 
not a single string.  There is no '[ or ]' in the string itself, and no
need to replace it with anything.

To print just that string, do

     print sys.argv[1] # fetch item

instead of

     print sys.argv[1:] # slice list

I suggest working through a tutorial or two.  Pay special attention to 
the difference between an object's value and the various ways that value 
can be output.

</F>




More information about the Python-list mailing list