Generating strings dynamically in Python
You're pretty much reinventing urllib.parse.urlencode:

from urllib.parse import urlencode


def prepare_query_string(**kwargs):
    return urlencode([(key, value) for key, value in kwargs.items() if value is not None])


Usage being:

&gt;&gt;&gt; prepare_query_string(active=1)
'active=1'
&gt;&gt;&gt; prepare_query_string(active=1, user=None)
'active=1'
&gt;&gt;&gt; prepare_query_string(active=1, user='bob')
'active=1&amp;user=bob'
&gt;&gt;&gt; prepare_query_string(file='foo.tar.gz', user='bob')
'file=foo.tar.gz&amp;user=bob'
&gt;&gt;&gt; prepare_query_string(file='foo.tar.gz', user='bob', active=None)
'file=foo.tar.gz&amp;user=bob'
&gt;&gt;&gt; prepare_query_string(file='foo.tar.gz', user='bob', active=1)
'file=foo.tar.gz&amp;user=bob&amp;active=1'