-1

I am trying to make the following print , but cannot make this print goes from 1 to 100

 for name in range(1,100):

 print(/html/body/div[3]/div/div/div[2]/ul/div/li[name]/div/div[1]/div[2]/div[1]/a)

The idea is to print those all these string value 100 times which goes from 1 to 100 inside li

Lucas Dresl
  • 1,150
  • 1
  • 10
  • 19

2 Answers2

3

Your indent is not correct in the second line, and your string is not correct.

This is my solution

for name in range(1,100):
    print("/html/body/div[3]/div/div/div[2]/ul/div/li[" + str(name) + "]/div/div[1]/div[2]/div[1]/a")

You can reference here for more ways how to add variable in a string How do I put a variable inside a String in Python?

Ha. Huynh
  • 1,772
  • 11
  • 27
2

similar as below with f-string formatting

for name in range(1,101):
    print(f"/html/body/div[3]/div/div/div[2]/ul/div/li[{name}]/div/div[1]/div[2]/div[1]/a")

updated: 1. correct the range(1, 101) 2. f-string was supported since Python 3.6+

xuemind
  • 405
  • 3
  • 11