Which makes sense -1 means doesn’t contain the character 1 means it does, but when I use it the find attribute for numbers and anything with a dash in it, I get a value greater than 1.
b= ‘dog-cat’
b.find( ‘at’)
result is 5
‘dog-cat’.find( ‘at’)
result is 5
‘2008-2009’.find( ‘2009’)
result is 5
num=‘2008-2009’
num.find( ‘2009’)
result is 5
num.find( ‘32’)
result is -1
Why do I get a value greater than 1 when the character is found but only a -1 when the value is not.
Also, I getting 0 when the character are within the string? Am I doing something wrong or is this an issue with Python.
Right, that’s how that string method is designed. From:
string.find(s, sub[, start[, end]])Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure.
[QUOTE=Carlo890;6589]
a=‘dog’
a.find( ‘og’)
result is 1
[/QUOTE]
If you changed the 2nd line to a.find(‘g’), you’ll get 2 because it is in the 2nd element of the string. String indexes atart at 0. Using dashes or numbers in your string doesn’t change the expected output of the function.
[QUOTE=Adam Pletcher;6592]Right, that’s how that string method is designed. From:
string.find(s, sub[, start[, end]])Return the lowest index in s where the substring sub is found such that sub is wholly contained in s[start:end]. Return -1 on failure.[/QUOTE]
I read that when I found the find attribute but I did not understand what it meant, or better yet didn’t read it thorough enough to understand it… thank you all for the help