Zed Fang Zed Fang - 25 days ago 9
Python Question

This code keeps giving me: string indices must be integers

I keep getting a TypeError: string indices must be integers. Not sure how to correct this.

def get_next_target(string):
start_str=string.find('<')
if start_str==-1:
return None,0

end_str=string.find('>',start_str)
next_start_str=string.find('<',end_str)

if next_start_str==-1:
return string[end_str+1:]

word=string[end_str+1,next_start_str]
return word,next_start_str



print (get_next_target('<h1>Title <>'))

Answer

You are trying to use a , for string slicing, which is causing this to become a tuple. You need to replace the , with a :

word=string[end_str + 1:next_start_str]

Comments