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

Зміст курсу

Типи даних у Python

Типи даних у Python

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

book
Negative Indexation

You can access characters in a string using negative indexing, which starts from the end of the string.

This is especially useful when you want to get the last character or work backwards, without manually counting the total length of the string.

Syntax

python

Negative indexing starts at -1. string[-1] returns the last character, string[-2] returns the second-to-last, and so on. string[-0] is the same as string[0] (first character), so it's rarely used.

Example

123
document = "Invoice #34567" last_char = document[-1] print(last_char) # Output: 7
copy

In this case, we don't need to count the total number of characters to find the last one. We simply use -1 to get the final digit of the invoice number.

Завдання

Swipe to start coding

You are working with a financial record string. Use negative indexing to extract:

  1. The last character of the string;
  2. The second-to-last character.

Assign them to the variables last_char and second_last_char, respectively.

Рішення

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

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

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

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

book
Negative Indexation

You can access characters in a string using negative indexing, which starts from the end of the string.

This is especially useful when you want to get the last character or work backwards, without manually counting the total length of the string.

Syntax

python

Negative indexing starts at -1. string[-1] returns the last character, string[-2] returns the second-to-last, and so on. string[-0] is the same as string[0] (first character), so it's rarely used.

Example

123
document = "Invoice #34567" last_char = document[-1] print(last_char) # Output: 7
copy

In this case, we don't need to count the total number of characters to find the last one. We simply use -1 to get the final digit of the invoice number.

Завдання

Swipe to start coding

You are working with a financial record string. Use negative indexing to extract:

  1. The last character of the string;
  2. The second-to-last character.

Assign them to the variables last_char and second_last_char, respectively.

Рішення

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

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

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

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