Skip to Content

How To Get First And Last N Characters From A String In Python

How To Get First And Last N Characters From A String In Python

It can be quite easy to get the first character in a string, but how can you get the first n characters from a string? Or what about the last n characters?

To get more than one character from a string you can use the slice operator which has the syntax [start:stop:step]. For example, to get the first 5 characters from a string variable my_string use the slice operator as follows my_string[:5] or to get the last 5 characters from a string use my_string[-5:].

Let’s look at this handy utility called the slice operator and see how it is versatile enough to be able to capture whatever you need from a string.

What Is The Slice Operator?

The slice operator contains three parameters to help capture the needed characters from a string and I’ve labelled these variables in the structure as [start:stop:step].

The first parameter to the slice operator syntax I’ve labelled as start and represents the starting index position for when capture begins from the source string and is inclusive of that character. The second parameter I’ve labelled stop is the ending index position of the capture from the source string and is exclusive of that character. Finally, the third parameter I’ve labelled step is the capture jump from start to end jumping at the step frequency.

Let’s look at a simple example using a unique word which has the highest frequency of unique characters (uncopyrightable):

>>> my_string = "Uncopyrightable
>>> my_string[0:5:1]
'Uncop'

As you can see from the above example the slice operator starts at index reference 0 which is the first character in a string, and the capture continues through to index reference 5 (being the sixth character) but does not include that character. Finally, the capture from start to end includes all characters as the jump is 1. This final step variable was not needed and the slice operation of my_string[0:5] would have produced the same result.

Besides the step variable having a default value, the start and stop variables also have default settings should they not be defined.

Have a look at the following examples and see if you can work out what happens if they are not defined in their slice operation:

>>> my_string = "Uncopyrightable"
>>> my_string[:5]
'Uncop'
>>> my_string[10:]
'table'

What happens when you don’t define a start variable in the slice operator?

As you can see from the above example, it assumes the capture starts from the very beginning of the string and proceeds all the way to the stop index position.

What happens when you don’t define a stop variable in the slice operator?

As you can also see from the above example, it assumes the capture proceeds from the starting index position and proceeds all the way to very end of the string.

What happens when you don’t define a start and a stop variable in the slice operator?

If you’ve seen the pattern from the above example, you should be able to surmise what would happen when neither is defined. Let’s see what happens:

>>> my_string = "Uncopyrightable"
>>> my_string[:]
'Uncopyrightable'

As expected it starts the capture at the beginning and proceeds through to the end of the string.

So how can the slice operator also be used to get the first n characters from a string?

How To Get The First n Characters From String

If you can get the first character from a string in Python using the slice operator then it doesn’t take much imagination on how you can fetch the first n characters.

The slice operator’s first variable sets the start capture and the second variable sets where the capture needs to stop (excluding that character from the capture).

Therefore to capture the first n characters from a string using the slice operator you would use the syntax: my_string[:n].

Here are some examples demonstrating this use case where I’m trying to capture the first 6 characters from a string:

>>> my_string = "Uncopyrightable"
>>> my_string[:6]
'Uncopy'

As you can see from the example above the capture of the first 6 characters from the original string produces the desired result.

This type of capture using my_string[:n] may be the more preferred method when finding the first or nth character from a string as the code is quite easy to understand.

How To Get The Last n Characters From String

Similar to how you can capture the first set of characters from a string, Python enables you to use the slice operator to capture the tail characters in a string.

To capture the last n characters from a string use the slice operator like so:

>>> my_string = "Uncopyrightable"
>>> my_string[-3:]
"ble"

As you can see from the above example the last three characters are captured from the original my_string variable by using a negative value in the start parameter of the slice operator.

When a negative number is used in either the start or stop parameter of the slice operator the character sourced is counted from the end of the string. Therefore, the last character in the string is denoted as index number -1, the second last as -2 (etc).

Therefore, to capture the last n characters from the string use the slice operator and its syntax:

source_string[-n:]

Get Both First & Last Characters

To get both first and last n characters from a string simply concatenate the two strings together using the slice operator from each approach.

For example if you need the first and last 3 characters from a source string as it’s own string then you could simply write the following:

>>> my_string = "Uncopyrightable"
>>> my_string[:3] + my_string[-3:]
"Uncble"

As you can see the result from combining both the first 3 characters and the last characters produces a new string of Uncble – whatever that is!

Summary

To capture the first n characters from a string use the powerful Python slice operator source_string[:n]. Conversely, to capture the last n characters from a string using the slice operator accordingly: source_string[-n:].