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 of the string in Python use the syntax
. The square brackets are used to indicate a specific character by index number, and as string data types can be iterated, you can use the
inside to capture the last character.
Here’s the code:
And here’s a quick demonstration this in operation:
>>> my_string = "How long is a piece of string?"
As can be seen from the above code, the final character in the example string used is the question mark.
This type of operation used on the original string is known as slicing as it uses the slice operator .
What Is The String Slice Operation?
has the format
is the first
item to start capture (
is the ending
item to stop capture, and
is the step or jump taken from the starting index to the ending index number.
terms in the expression. If the
parameter is empty the operation captures from the
end of the string
. If the
parameter is empty the operation uses
as the step value.
parameter defined, for example,
the slice operator starts its capture from index
which is the last character of a string. As the
parameter is empty, it will capture to the end of a string, returning just the last character.
Use Positive Indexes
You don’t have to use the slice notation on your strings with
index reference. You can also use the slice operator with positive indexes, however, it is more code to obtain the last index number of your string.
As you need to find the length of your string, you will need the help of the
function and then by subtracting
from this result, you’ll be converting it to an
number which the slice operator uses.
Here’s the code:
Here’s why you need to subtract 1 from the total length of the string, notice the index number of each character in the Python string:
Conversely, if we do the same operation but use a negative number in the slice operator, here’s what you will get:
Notice also in the last video the use of the function
range(start, stop, step)
function is the shorthand equivalent to the slice operator
As you’ve seen from both demonstrations of positive and negative indexes you can easily extract the last character in a string using
with a negative index, or, if you wanted to extract the last character using a
index, it would be the length of the string minus 1.
A visual representation of the index number of a string can be seen from the below picture:
What Happens When String Is Empty?
If you have an empty string and try to perform a slice operation on it you will get an
error, as demonstrated:
To alleviate this error, you can apply several different approaches:
Use a boolean expression as an empty string is
The first approach can be quite easy to implement, but it should be encouraged to check the data type of the variable, and that is of a
data type, otherwise you will get
You can do this by using
type(my_string) == str
, here’s an example:
if my_string and type(my_string) == str:
print("my_string is an empty string")
The other approach is to wrap your code in a
block, but these tend to be more computationally expensive than the other alternative:
except IndexError as e:
print("my_string is empty")
Here’s how both approaches work:
How To Get Last
Characters From String
Knowing a little on how the
works you can apply your knowledge on extracting just the last
characters of a string (rather than just the last).
To use the
on a string to fetch the last
characters would mean starting from the last nth character and capturing all other individual characters to the end.
Staying with the use of negative indexes, you can increase that number to start the capture further back from the end.
One use case I had for this technique was capturing the postcodes of physical addresses. A data set of addresses was written in the following format:
StreetNumber StreetName StreetType, Suburb STATE PostCode
102 Smith Street, Sydney NSW 2000
With this slicing technique using negative indexes, I was able to extract the post code by using this code:
>>> address = "102 Smith Street, Sydney NSW 2000"
>>> post_code = address[-4:]
Here’s a demonstration of this code in action:
Notice, though, one little detail with the slice operator that can be easily missed.
Notice I didn’t write this:
post_code = address[-4]
As this would have just returned the
position of the 4th last character from the string, being
. To ensure I capture everything to the end I need to make the
parameter empty, which is why
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 fourth last variable through to the end.
Get Last Character From String In Python
To fetch the last character in a string use the slice operator by using
is the variable storing the string you want to fetch the last character from.
This is a quick handy technique to help you perform other types of string manipulation.
To fetch the last
characters of the string using the slice operator 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 n characters of a string using Python .