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:

  • 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 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 Python Strings: 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, 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 Strings: 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 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 Slicing Python Strings

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

More Resources

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

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
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

10 thoughts on “Slicing Python Strings: A Complete Guide

Leave a Reply

%d bloggers like this: