Search code examples
pythonargvsys

Remove brackets from argv string


I am new to Python. I'm running Raspbian and calling a Python script this way:

python testarguments.py "Here is a test parameter"

My script:

import sys
print sys.argv[1:]

Output:

['Here is a test parameter']

Question: What is the most efficient way to remove beginning and ending brackets and single quotes from sys.argv output?


Solution

  • You are slicing with

    sys.argv[1:]
    

    It means that get all the elements from 1 till the end of the sequence. That is why it creates a new list.

    To get only the first item, simply do

    sys.argv[1]
    

    This will get the element at index 1.