Estoy tratando de obtener el índice de la primera aparición de un carácter que aparece en una cadena después de un índice específico. Por ejemplo:
string = 'This + is + a + string' # The 'i' in 'is' is at the 7th index, find the next occurrence of '+' string.find_after_index(7, '+') # Return 10, the index of the next '+' character >>> 10Python es tan predecible:
>>> string = 'This + is + a + string' >>> string.find('+',7) 10 help(str.find) :
find(...) S.find(sub[, start[, end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure. También funciona con str.index excepto que esto generará raise ValueError en lugar de -1 cuando no se encuentra la subcadena.
Puedes usar:
start_index = 7 next_index = string.index('+', start_index)string.find('+', 7)Leer la documentación .