×

Slicing Python Strings

Python comes with a string slicing system, Slicing is nothing but fetching a piece from a pie. Square 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:

  • Slicing a Single Character from a Python String
  • Using a Negative Index While Slicing Python Strings
  • Slicing Python Strings by Position
  • Slicing a Python String for Every n Indices

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 Programming.

s = "Programming"

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, g.

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

Negative Indices

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, n.

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

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, rog. 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, Pr.

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

Slicing 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 Pormig. Next, we’ll slice from index zero to seven by three or Pgm. 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, argo. Finally, we’ll cover slicing from the second to last index to the fifth to last index by two or nm.

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

Summary of Slicing Python Strings

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.