alvinfungg
alvinfungg

Reputation: 27

python increment number in the URL

is there any possible way to have a increment of two different numbers in the URL that I have? One f the number will be starting with -1. Second number will be starting with 0.

url = "https://mywebsite.com/amp"
for count in range(-1, 5):
    print(url + str(count)) 

this gives me

https://mywebsite.com/amp-1
https://mywebsite.com/amp0
https://mywebsite.com/amp1
https://mywebsite.com/amp2
https://mywebsite.com/amp3
https://mywebsite.com/amp4

I'm trying to achieve this

https://mywebsite.com/amp=-1&id=0
https://mywebsite.com/amp=0&id=1
https://mywebsite.com/amp=1&id=2
https://mywebsite.com/amp=2&id=3
https://mywebsite.com/amp=3&id=4

Upvotes: 0

Views: 704

Answers (2)

Doron Cohen
Doron Cohen

Reputation: 61

Using str.format():

for n in range(-1, 4):
    print('{}?amp={}&id={}'.format(url, n, n+1))

Upvotes: 1

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181290

Try this:

url = "https://mywebsite.com/amp"
for count in range(-1, 5):
    print(f"{url}/amp={count}&id={count+1}")

Output:

https://mywebsite.com/amp/amp=-1&id=0
https://mywebsite.com/amp/amp=0&id=1
https://mywebsite.com/amp/amp=1&id=2
https://mywebsite.com/amp/amp=2&id=3
https://mywebsite.com/amp/amp=3&id=4
https://mywebsite.com/amp/amp=4&id=5

Do keep in mind that if you want the query string to pass values as proper parameters, you need to add a ? after the first /amp. Like this:

https://mywebsite.com/amp?amp=-1&id=0

Upvotes: 3

Related Questions