Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Negative Indexing and Length | Variables and Types
course content

Зміст курсу

Introduction to Python

Negative Indexing and LengthNegative Indexing and Length

You can also index from the end of the string. In this case, the last element has an index of -1, the second-to-last is -2, and so on.

Image

Note

Keep in mind that -1 is the go-to way to access the last element.

Now that you're familiar with referencing characters in a string, you might wonder how to determine the total number of characters in that string. Fortunately, Python provides the len() function for that. Just pass the string or the variable holding the string as an argument.

As you might notice, the length of a string corresponds to the number of characters in it. Thus, the total count of characters in a string is always one greater than the index of its last character. For instance, if the length of a string is 10, the index of its last character is 9.

Note

You can also determine the index of the last character by using len(string) - 1.

For the string test provided below, identify the character at index -4.

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 9
course content

Зміст курсу

Introduction to Python

Negative Indexing and LengthNegative Indexing and Length

You can also index from the end of the string. In this case, the last element has an index of -1, the second-to-last is -2, and so on.

Image

Note

Keep in mind that -1 is the go-to way to access the last element.

Now that you're familiar with referencing characters in a string, you might wonder how to determine the total number of characters in that string. Fortunately, Python provides the len() function for that. Just pass the string or the variable holding the string as an argument.

As you might notice, the length of a string corresponds to the number of characters in it. Thus, the total count of characters in a string is always one greater than the index of its last character. For instance, if the length of a string is 10, the index of its last character is 9.

Note

You can also determine the index of the last character by using len(string) - 1.

For the string test provided below, identify the character at index -4.

Виберіть правильну відповідь

Все було зрозуміло?

Секція 2. Розділ 9
some-alt