How do you get the last character from a string using Python? Thankfully Python has an easy way to fetch the last character from a string using the slice operator .
To get the last character from a string in Python use the syntax
. The square brackets are used to indicate a specific reference to a character by index number, and using -1 denotes the last character.
Here is an example demonstrating this code and how it is used to extract the final character from the string:
>>> my_string = "How long is a piece of string?" >>> my_string[-1] '?'
As can be seen from the above code the final character is the question mark.
The operation used on the string is known as the slice operator .
has the format
is the first indexed item to begin capture,
is the ending indexed item to stop capture (excluding that item) and
is the step or jump taken from the starting index to the ending index number (if no value is placed for
then 1 is assumed).
In the example above where only one value is inserted it would be the equivalent of the following if two or more of the slice operator variables were used:
>>> my_string = "How long is a piece of string?" >>> my_string[-1:] '?' >>> my_string[-1::] '?' >>> my_string[-1::1] '?'
As you can see from the examples above demonstrating the same output using the slice operator the results are exactly the same.
Therefore, the first example used is the
syntax possible to get the
But what if you need to capture the last
characters from a string, can the same technique apply? Is it versatile enough to handle more use cases?
How To Get Last
Characters From String
You apply the same technique to get the last
characters from a string as you did with fetching the last character, however, an extra character in the slice operator will be needed.
To use the
on a string to fetch the last
characters would mean starting from the last nth character and capturing all to the end.
Here’s how this would look with the operator if the use case was to capture the last 5 characters:
>>> my_string = "How long is a piece of string?" >>> my_string[-5:] 'ring?'
As you can see from the example above to capture the last
characters from a string you just need to set the first parameter in the
to the negative value of
required and then set the second parameter blank.
By setting the second parameter blank it implies the capture will go all the way to the end, and because no value was used for the third parameter (the step number) it captures everything from the fifth last variable through to the end.
To fetch the last character in a string use the slice operator by writing
is the variable storing the string you want to fetch the last character from.
To fetch the last
characters from a string using the slice operator again but instead of using -1 as the value use a negative
number according to the number of characters you want to capture. For example,
will capture the last 5 characters in a string whose variable is
Next, you might want to explore our other post on how to get the first character from a string using Python .