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
string[-1] # Last character
string[-2] # Second-to-last character
string[-3] # Third-to-last character
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
123document = "Invoice #34567" last_char = document[-1] print(last_char) # Output: 7
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:
- The last character of the string;
- The second-to-last character.
Assign them to the variables last_char
and second_last_char
, respectively.
Lösung
Danke für Ihr Feedback!
single
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Awesome!
Completion rate improved to 3.03Awesome!
Completion rate improved to 3.03
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
string[-1] # Last character
string[-2] # Second-to-last character
string[-3] # Third-to-last character
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
123document = "Invoice #34567" last_char = document[-1] print(last_char) # Output: 7
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:
- The last character of the string;
- The second-to-last character.
Assign them to the variables last_char
and second_last_char
, respectively.
Lösung
Danke für Ihr Feedback!
single
Awesome!
Completion rate improved to 3.03
Negative Indexation
Swipe um das Menü anzuzeigen
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
string[-1] # Last character
string[-2] # Second-to-last character
string[-3] # Third-to-last character
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
123document = "Invoice #34567" last_char = document[-1] print(last_char) # Output: 7
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:
- The last character of the string;
- The second-to-last character.
Assign them to the variables last_char
and second_last_char
, respectively.
Lösung
Danke für Ihr Feedback!