Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Searching a String | Strings
Python Data Types

book
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.

string = '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}')
1234
string = '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}')
copy

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.

Aufgabe

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.

Lösung

string = "apple, banana, cherry, orange"
position_a = string.find('a')
position_h = string.find('h')
position_y = string.find('y')
position_Y = string.find('Y')
print(f'The position of "a" is {position_a } and the position of "h" is {position_h }')
print(f'The position of "y" is {position_y } and the position of "Y" is {position_Y }')

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 10
string = "apple, banana, cherry, orange"
position_a = string._ _ _
position_h = string._ _ _
position_y = string._ _ _
position_Y = string._ _ _
print(_ _ _'The position of "a" is {_ _ _} and the position of "h" is {_ _ _}')
print(_ _ _'The position of "y" is {_ _ _} and the position of "Y" is {_ _ _}')
toggle bottom row
some-alt