Parsing A List Into A Url String
I have a list of tags that I would like to add to a url string, separated by commas ('%2C'). How can I do this ? I was trying : >>> tags_list ['tag1', ' tag2'] >>>
Solution 1:
parse_string = ("http://www.google.pl/search?q=%s&restofurl" %
'%2C'.join(tag.strip() for tag in tags_list))
Results in:
>>>parse_string = ("http://www.google.pl/search?q=%s&restofurl" %...'%2C'.join(tag.strip() for tag in tags_list))>>>parse_string
'http://www.google.pl/search?q=tag1%2Ctag2&restofurl'
Side note:
Going forward I think you want to use format()
for string interpolation, e.g.:
>>>parse_string = "http://www.google.pl/search?q={0}&restofurl".format(...'%2C'.join(tag.strip() for tag in tags_list))>>>parse_string
'http://www.google.pl/search?q=tag1%2Ctag2&restofurl'
Post a Comment for "Parsing A List Into A Url String"