user3038725
user3038725

Reputation: 321

for loop range values

Please see my code below

for i in range (0,3):
    url = "number/i"
    print url

My expectation is

number/1
number/2
number/3

Can I also change URL to URL1= number/1 URL2=number/2 as well?

Upvotes: 0

Views: 56

Answers (2)

CDspace
CDspace

Reputation: 2689

Pythons range defaults to starting at 0, and runs to one less that the stop value, in your case it will give 0, 1, 2. You can either add one to get the values you want, or adjust your range to range(1, 4)

Then, to get the printed output you want, you'll need to convert the number to a string, like so

url = "number/" + str(i)

Upvotes: 2

barak manos
barak manos

Reputation: 30126

Change:

url = "number/i"

To:

url = "number/"+str(i+1)

Upvotes: 0

Related Questions