Slicing Python Strings: A Complete Guide

Python comes with a string slicing system that is a Swiss army knife of functionality. Bracket notation allows us to slice Python strings to get one or more characters in order or in reverse. In this complete guide on slicing Python strings we’ll cover:

Before we get into how to slice Python strings, we have to have some context on the string to slice. So the first thing we’ll do in our program is declare the string. For this example, let’s use abcdefg.

s = "abcdefg"

Slicing Python Strings: A Single Character

The most basic way to slice Python strings is to slice for a single character. Remember that strings start at index 0. In the example below we are slicing for index number 3, which is the fourth character, d.

# any character
print(s[3]) # expect d

Slicing Negative Indices in Python Strings

Unlike Java or C#, negative indices are permitted in Python. Negative indices are a neat trick that allow you to access the character n from the end. For example, in the line below we’ll access the character second from the end, f.

# negative indices
print(s[-2]) # expect second to last = f

Slicing Python Strings: Substrings By Position

Now let’s take a look at slicing Python strings by position. You can slice by position in Python using the colon operator. We’ll cover two examples of how to slice by position. 

First, we’ll slice from positions one to four, which will get us entries one, two and three, bcd. Python string slicing is inclusive for the first index, but exclusive on the second one. For our second example, we’ll leave the first index blank, which will default our slice to the start of the string, index zero. This will get us the first two entries, ab.

# slicing positions
print(s[1:4]) # expect 1, 2, 3 = bcd
print(s[:2]) # expect beginning to 1 = ab

Slicing Python String Positions n at a time

Lastly, let’s take a look at slicing Python strings for every nth entry. We do this by adding a second colon inside the brackets. Here we’ll cover four examples of slicing Python strings for every nth entry.

First, we’ll slice from index zero to the end by two, which should get us aceg. Next, we’ll slice from index zero to seven by three or adg. Note that there are seven total letters in the string which means there are only indices zero to six. However, the closing end of indices when slicing strings in Python is open so we end our slice on seven.

Third, we’ll slice backwards from indices five to one by negative one, fedc. Finally, we’ll cover slicing from the second to last index to the fifth to last index by two or fd.

# slicing positions _n_ at a time
print(s[0::2]) # expect every 2 from 0 to end = aceg
print(s[0:7:3]) # expect every 3 from 0 to 6 = adg
print(s[5:1:-1]) # expect reverse entries from 5 to 2 = fedc
print(s[-2:-5:-2]) # expect reverse every other entry from second to fifth to last = fd

Results from Python String Slicing

When we run all those lines for the string abcdefg, we should get the results as commented. The output will look like the image below.

Results for slicing Python Strings examples
Slicing Python Strings Example Outputs

Summary of String Slicing in Python

In this post we went over how to slice Python strings forward, backwards, and while skipping count. First, we learned how to slice a single character out of a string. Then we looked at using negative indices. Next, we looked at how to slice substrings by position and for every nth index.

More Resources

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang

10 thoughts on “Slicing Python Strings: A Complete Guide

Leave a Reply

%d bloggers like this: