Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Get the Index of the Symbol | Рядки
Типи даних у Python

book
Get the Index of the Symbol

To find the index of a specific symbol or substring in a string, Python provides the built-in .find() method. This method returns the index of the first occurrence of the substring. If the substring is not found, it returns -1.

python
string.find(substring)

Here, string is the original text, and substring is the character or sequence you want to locate.

string = "Channel efforts to success!"
print(string.find('n'))
12
string = "Channel efforts to success!" print(string.find('n'))
copy

In the string "Channel efforts to success!", the first 'n' appears at index 3. So, the find() method returns 3.

If the character were not found, for example, string.find('z'), the result would be -1.

Завдання

Swipe to start coding

Hone your skills as much as possible!

So here you should find the index of the letters p, y, and r.

Рішення

string = "Work your way up!"

# Find the index of the letter `p`
p = string.find('p')

# Find the index of the letter `y`
y = string.find('y')

# Find the index of the letter `r`
r = string.find('r')

print("Variable p equals:", p)
print("Variable y equals:", y)
print("Variable r equals:", r)
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 9
single

single

string = "Work your way up!"

# Find the index of the letter `p`
p = string.find(___)

# Find the index of the letter `y`
y = ___.___('y')

# Find the index of the letter `r`
r = ___

print("Variable p equals:", p)
print("Variable y equals:", y)
print("Variable r equals:", r)

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

some-alt