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
my_string[-1]. 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.
The slice operator has the format
x is the first indexed item to begin capture,
y is the ending indexed item to stop capture (excluding that item) and
z is the step or jump taken from the starting index to the ending index number (if no value is placed for
z 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 shortest syntax possible to get the
my_string[-1] using the slice operator.
But what if you need to capture the last
n characters from a string, can the same technique apply? Is it versatile enough to handle more use cases?
How To Get Last
n Characters From String
You apply the same technique to get the last
n 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 slice operator on a string to fetch the last
n 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
n characters from a string you just need to set the first parameter in the slice operator to the negative value of
n 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
my_string is the variable storing the string you want to fetch the last character from.
To fetch the last
n characters from a string using the slice operator again but instead of using -1 as the value use a negative
n number according to the number of characters you want to capture. For example,
my_string[-5:] 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.