Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

problem with IndexError: string index out of range

$
0
0
So my code is the following:

def correction(search_str, target_str):
	i = 0
	new_string = ''
	while True:
		if search_str.find(target_str[i]) != -1:
			new_string = new_string + target_str[i]
		else: break
		i = i + 1
	return new_string



What i am trying to do is to find some spread letters in a text(search_str) and form a word(target_str) out of them if it exists. How ever i keep on getting "IndexError: string index out of range" for the line:

search_str.find(target_str[i]) != -1:


what am i doing wrong?

Kind regards

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>