1

simple question.

If I have this line:

      j = json.load(urllib.urlopen("http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q= ***$???*** "))

I'm trying to pass an argument into that spot from another script +/or the command line.

In bash its simple, $1, $2, etc etc.

I've read about (sys.argv[1]) and I'm missing it because such a simple thing in bash seems to need such verbose-ness in python. I've tried this:

      j = json.load(urllib.urlopen("http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q='(sys.argv[1])'"))

To no avail.... What am I not understanding?

2 Answers 2

4

Strings are not interpolated by default in Python. Try

("http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q='%s'"
 % sys.argv[1])
Sign up to request clarification or add additional context in comments.

3 Comments

if i had not asked on here... how would i have come upon that tidbit? what's the understand behind that...it worked btw, i'll check it once time is cleared.
@sirvonandrethomas: the fact that some other languages magically interpolate strings doesn't mean Python should do that. I think % is actually in the tutorial.
0

Yep, for completeness, there's "new formatting" which is a bit like Java's format strings, and a little nicer to work with.

 $ python -c 'import sys; print("yojo{0}".format(sys.argv[1]))' mo
yojomo

And if you just want to concatenate strings, the + operator is overloaded for that.

print("mystring" + sys.argv[1])

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.