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

Зміст курсу

Типи даних у Python

Типи даних у Python

1. Знайомство з числами в Python
2. Істина чи брехня?
3. Рядки
4. Поєднання всіх тем разом

book
String Indexing in Python

String indexing allows you to access individual characters of a string using their position, or index.

Indexing in Python starts from 0. That means the first character of any string has index 0, the second character has index 1, and so on.

For example, in the word "codefinity":

To access a character by index, use square brackets [] with the index number inside:

12345
string = "codefinity" print("Print the c symbol:", string[0]) # First character print("Print the o symbol:", string[1]) # Second character print("Print the y symbol:", string[9]) # Last character
copy

Index Out of Range

If you try to access a character at an index that does not exist, Python will raise an error:

12
# This will raise an IndexError print(string[10])
copy

This is called an "index out of range" error. In the word "codefinity", valid indexes are from 0 to 9, because it has 10 characters in total.

Завдання

Swipe to start coding

In accounting systems, initials are often used instead of full names when generating reports or signatures.

You are given the variables first_name and last_name, which store a person’s name.

Use string indexing only to extract the first letters from each name and display them in the format: M.J.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

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

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

Секція 3. Розділ 3
toggle bottom row

book
String Indexing in Python

String indexing allows you to access individual characters of a string using their position, or index.

Indexing in Python starts from 0. That means the first character of any string has index 0, the second character has index 1, and so on.

For example, in the word "codefinity":

To access a character by index, use square brackets [] with the index number inside:

12345
string = "codefinity" print("Print the c symbol:", string[0]) # First character print("Print the o symbol:", string[1]) # Second character print("Print the y symbol:", string[9]) # Last character
copy

Index Out of Range

If you try to access a character at an index that does not exist, Python will raise an error:

12
# This will raise an IndexError print(string[10])
copy

This is called an "index out of range" error. In the word "codefinity", valid indexes are from 0 to 9, because it has 10 characters in total.

Завдання

Swipe to start coding

In accounting systems, initials are often used instead of full names when generating reports or signatures.

You are given the variables first_name and last_name, which store a person’s name.

Use string indexing only to extract the first letters from each name and display them in the format: M.J.

Рішення

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

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

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

Секція 3. Розділ 3
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Ми дуже хвилюємося, що щось пішло не так. Що трапилося?
some-alt