I have written a python code which gives the following error "IndexError: string index out of range"
Please tell me how to fix this error.
actuallink = 'http://www.exxonmobilperspectives.com'
slashcounter = 0
indexslash = 0
while slashcounter < 3:
if(actuallink[indexslash] == '/'):
slashcounter = slashcounter + 1
indexslash = indexslash + 1
PLink = actuallink[:indexslash - 1]
PS. When I change the link to anything else, it works perfectly
Something like
actuallink = 'http://www.exxonmobilperspectives.com'
endPoint = len(actuallink.split('/')) - 1
if endPoint > 0:
slashcounter = 0
indexslash = 0
while slashcounter < endPoint:
if(actuallink[indexslash] == '/'):
slashcounter = slashcounter + 1
indexslash = indexslash + 1
PLink = actuallink[:indexslash]
Try this:
actuallink = 'http://www.exxonmobilperspectives.com'
slashcounter = 0
indexslash = 0
while indexslash < len(actuallink):
if(actuallink[indexslash] == '/'):
slashcounter = slashcounter + 1
print("Slash number {},Index ={}".format(slashcounter,indexslash))
indexslash = indexslash + 1
PLink = actuallink[:indexslash - 1]
print("Slashcounter = {}".format(slashcounter))
The result :
Slash number 1,Index =5
Slash number 2,Index =6
Slashcounter = 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.