I want to find the index of the first element in a string that is not equal to a given value
Pseudo code:
string='111111234131986'
string.find(!='1')
Result:
6
Here is a very simple solution using lstrip()
and len()
:
len(string) - len(string.lstrip("1"))
This solution returns len(string)
if string
is empty or entirely composed by "1"s.
Assuming you want the first element that isn't '1'
, we can use next()
and enumerate()
>>> string='111111234131986'
>>> next((i for i, x in enumerate(string) if x!='1'), None)
6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With