Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a string into a url python

I am trying to add a string in the middle of an url. Somehow my output looks like this:

http://www.Holiday.com/('Woman',)/Beach
http://www.Holiday.com/('Men',)/Beach

Somehow it should look like this:

http://www.Holiday.com/Woman/Beach
http://www.Holiday.com/Men/Beach

The code which I am using looks like the following:

list = {'Woman','Men'}
url_test = 'http://www.Holiday.com/{}/Beach'
for i in zip(list):
    url = url_test.format(str(i))
    print(url)
like image 449
MCM Avatar asked Feb 05 '23 03:02

MCM


2 Answers

Almost there. Just no need for zip:

items = {'Woman','Men'} # notice that this is a `set` and not a list
url_test = 'http://www.Holiday.com/{}/Beach'
for i in items:
    url = url_test.format(i)
    print(url)

The purpose of the zip function is to join several collections by the index if the item. When the zip joins the values from each collection it places them in a tuple which it's __str__ representation is exactly what you got. Here you just want to iterate the items in the collection

like image 169
Gilad Green Avatar answered Feb 07 '23 16:02

Gilad Green


You can try this also, And please don't use list as a variable name.

lst = {'Woman','Men'}
url_test = 'http://www.Holiday.com/%s/Beach'
for i in lst:
     url = url_test %i
     print url
like image 27
Rahul K P Avatar answered Feb 07 '23 16:02

Rahul K P