Searching a String
We also have the ability to search for substrings in strings using find()
method. This method returns the index of the first occurrence of the substring. If there are no lines, then the function will return -1
.
1234string = 'Jackfruit, Coconut' position_1 = string.find('C') position_2 = string.find('c') print(f'The position of "C" is {position_1}, and the position of "c" is {position_2}')
Don't forget Python is case sensitive.
So, the first occurrence of the C
is 11,
and the first occurrence of the c
is 2. Since C
it is not the same as c
.
Swipe to start coding
You have such a string
string = "apple, banana, cherry, orange"
You have to find out the index of the first occurence of such substrings as:
a, h, y, Y
.
Solución
¡Gracias por tus comentarios!
single
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Awesome!
Completion rate improved to 3.85
Searching a String
Desliza para mostrar el menú
We also have the ability to search for substrings in strings using find()
method. This method returns the index of the first occurrence of the substring. If there are no lines, then the function will return -1
.
1234string = 'Jackfruit, Coconut' position_1 = string.find('C') position_2 = string.find('c') print(f'The position of "C" is {position_1}, and the position of "c" is {position_2}')
Don't forget Python is case sensitive.
So, the first occurrence of the C
is 11,
and the first occurrence of the c
is 2. Since C
it is not the same as c
.
Swipe to start coding
You have such a string
string = "apple, banana, cherry, orange"
You have to find out the index of the first occurence of such substrings as:
a, h, y, Y
.
Solución
¡Gracias por tus comentarios!
Awesome!
Completion rate improved to 3.85single