# Using Python Variables in one Instance

**URL:** <https://community.unix.com/t/using-python-variables-in-one-instance/344275>\
**Category:** Shell Programming and Scripting\
**Created:** [July 7, 2014, 5:14pm UTC](https://community.unix.com/t/using-python-variables-in-one-instance/344275 "2014-07-07T17:14:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![metallica1973](https://community.unix.com/letter_avatar/metallica1973/32/6_449c533f586939b6e5f246632a46421f.png) [@metallica1973](https://community.unix.com/u/metallica1973)\
**Post date:** [July 7, 2014, 5:14pm UTC](https://community.unix.com/t/using-python-variables-in-one-instance/344275/1 "2014-07-07T17:14:36Z")

</div>

I have another basic Pythonic question that I cant seem to figure out. Here we go. So what I am trying to accomplish is simply using a second variable in my "scanjob" variable adding "api\_tok". I am using a product that needs an api\_token for every call so I just want to persistently add "api\_tok" where needed. So far

```plaintext
    auths = requests.get('http://10.0.0.127:443', auth=('admin', 'blahblah'), headers = heads)
    api_tok = {'api_token=e27e901c196b8f0399bc79'}
    scanjob = requests.get('http://10.0.0.127:4242/scanjob/1?%s' % (api_tok))
    scanjob.url
    u"http://10.0.0.127:4242/scanjob/1?set(['api_token=e27e901c196b8f0399bc79'])"

```

as you can see from scanjob.url, its adding a "set" after the "?". Why? If I could remove that "set" my call will work. I tried many different variants of combining a string such as:

```plaintext
    scanjob = requests.get('http://10.0.0.127:4242/scanjob/1?%s' + api_tok)
    scanjob.url
    u"http://10.0.0.127:4242/scanjob/1?set(['api_token=e27e901c196b8f0399bc79'])"
    scanjob = requests.get('http://10.0.0.127:4242/scanjob/1?' + str(api_tok))
    scanjob.url
    u"http://10.0.0.127:4242/scanjob/1?set(['api_token=e27e901c196b8f0399bc79'])"

```

??

---------- Post updated at 05:14 PM ---------- Previous update was at 04:50 PM ----------

many thanks for your reply but I was able to figure it out with some help:

{....} is syntax to produce a set object:

```
As of Python 2.7, non-empty sets \(not frozensets\) can be created by placing a comma-separated list of elements within braces, for example: \{'jack', 'sjoerd'\}, in addition to the set constructor.

```

For example:

\>\>\> {'api\_token=e27e901c196b8f0399bc79'}  
set(['api\_token=e27e901c196b8f0399bc79'])  
\>\>\> {'jack', 'sjoerd'}  
set(['jack', 'sjoerd'])

This is where your mystery set([...]) text comes from.

You just wanted to produce a string here:

api\_tok = 'api\_token=e27e901c196b8f0399bc79'  
scanjob = requests.get('[http://10.0.0.127:4242/scanjob/1?%s](http://10.0.0.127:4242/scanjob/1?%25s)' % api\_tok)

Alternatively, tell requests to add the query parameters with the params keyword argument, passing in a dictionary:

parameters = {'api\_token': 'e27e901c196b8f0399bc79'}  
scanjob = requests.get('[http://10.0.0.127:4242/scanjob/1](http://10.0.0.127:4242/scanjob/1)', params=parameters)

This has the added advantage that requests now is responsible for properly encoding the query parameters.
