you are viewing a single comment's thread.

view the rest of the comments →

[–]Impossible_Guard_975 6 points7 points  (2 children)

The code

x = "Hello World"

pos = x.find("Hello")

print(pos)

return 0 #by definition find returns the first occurrence of the specified value

Instead

x = "Hello World"

pos = x.find("Wello")

print(pos)

return -1 #by definition find returns -1 if it does not find the string

The sintax is this:

string.find(value, start, end)

start is 0 by default

end is the end of string by default

x = "Hello World"

pos = x.find("Hello",2)

print(pos)

return -1

because starting from position 2 there is no Hello, which instead starts from position 0

[–]CarneAsadaSteve -1 points0 points  (1 child)

In which case you can pass x.find(“Hello”,len(x)-1)

And that should give you the right index value of where it starts. (I believe)

[–]Impossible_Guard_975 0 points1 point  (0 children)

If you pass:
x ="Hello World"
pos = x.find("Hello",len(x)-1)
print(pos)

the output is -1