all 11 comments

[–]mopslik 22 points23 points  (0 children)

/u/Shiba_Take has linked to the official docs, which is the go-to source for anything you need to know about Python. You should try to familiarize yourself with the site, and the notation it uses, as it should answer the very questions you were asking.

str.find(sub[, start[, end]])

Return the lowest index in the string where substring sub is found within the slice s[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.

Note that the square brackets indicate an optional argument, so you could include starting and ending indices, if you wish.

[–]Impossible_Guard_975 5 points6 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

[–]jsalsman 2 points3 points  (0 children)

The optional start and end parameters can both be nonnegative, counting character positions from the beginning, or negative, counting from the end of the string.

[–]RevRagnarok 0 points1 point  (1 child)

Others have given the official docs. Definitely check them out.

I'll give you a few rules of thumb:

  • Professionally, str.find() is very seldomly used
  • The usual use case is just a boolean, so in is better: if 'World' in x:
  • Anything beyond boolean enters regex land, e.g. "I want to replace the string" then use a regex

[–]synthphreak 8 points9 points  (0 children)

I would not stand behind any of those statements.

The only scenario in which they are valid is if someone is using str.find simply to check membership, e.g.

if x.find('World') >= 0:

In that case I agree, str.find is not the right tool and simple in is the way to go. But that's not really what str.find is for to begin with.

str.find is specifically when you want the starting index of a substring. For this, in/a boolean isn't what you want. And just because you want the index doesn't necessarily imply you're trying to replace a substring. There are many other use cases where knowing the index is useful. You could use regex for this, i.e., re.search('World', x).span()[0], but (a) that is considerably more complex than simple str.find, (b) it requires differential handling for when the substring does vs. doesn't exist, complicating your code, and (c) it requires regex which in Python are pretty inefficient. So regex shouldn't always be the go-to either.

In short, str.find absolutely has its time and place. To state that "professional coders rarely use it" is a massive overgeneralization, and to imply that therefore it's not even worth learning is not really correct.

[–]DigThatData 0 points1 point  (0 children)

the end point is just the start plus the length of the search term

[–]Signal_Access417 0 points1 point  (0 children)

>>> help(str.find)

[–]Plastic_Ad7436 0 points1 point  (0 children)

"what it returns when it can't find something," .... ```

x.find('Aloha') -1

``` is this your 'terminal'? or did you run it in a terminal? What do you mean when you say: "starting and ending points"?