DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk2 min

How to Perform String Slicing in Python

Python string slices use an inclusive start and exclusive stop. Learn the syntax, negative indices, steps, reversal, and how to build a modified copy.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use text[start:stop:step] to select part of a Python string. The start position is included and the stop position is excluded, so word[0:2] returns "Py" from "Python". Omitting bounds, using negative indices, or adding a step lets you select from the beginning, count from the end, or move backward.

Python string-slicing syntax

A slice has the form text[start:stop:step]. The colon separates its optional start, stop, and step values. With the usual positive step, Python moves from left to right, including the start position but stopping before the stop position. The stop is a boundary, not another selected character.

word = "Python"

word[0:2]  # "Py"
word[2:5]  # "tho"

Think of indices as positions between characters: word[0:2] selects the characters between boundaries 0 and 2. This rule also means adjacent slices join back together: text[:i] + text[i:] == text.

Omit bounds to slice from an edge

For a positive step, an omitted start means the beginning of the string, and an omitted stop means the end.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
word[:2]  # "Py"
word[4:]  # "on"
word[:]   # "Python"

Use negative indices to count from the end

Negative indices count backward from the string’s end: -1 is the final position, -2 the one before it, and so on.

word[-2:]  # "on"
word[:-1]  # "Pytho"

-0 is the same value as 0, so use -1, not -0, to refer to the last character.

Use a step to skip positions or reverse a string

The optional step controls how many positions to advance between selected characters. A positive step moves forward; a negative step moves backward. A step of zero is invalid.

word[::2]   # "Pto"
word[::-1]  # "nohtyP"

word[::-1] is the common full-string reversal idiom: the negative step traverses the string backward. With a negative step, omitted bounds follow the reverse traversal’s boundary behavior, so do not assume they behave just like omitted bounds with a positive step.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Out-of-range slices and indexing are different

Python adjusts slice bounds that go beyond the string’s length. For example, word[0:100] returns the whole string rather than raising an error. A positive-step slice whose start is at or beyond its stop is empty.

word[0:100]  # "Python"
word[4:2]    # ""

This forgiving behavior applies to slices, not ordinary single-position indexing: asking for a nonexistent position, as in word[100], raises IndexError.

Slicing makes a selection; it does not edit the string

Python strings are immutable, so slicing returns a string and cannot change the original in place. To replace a portion, concatenate the unchanged parts with the replacement to make a new string:

text = "Python"
start, stop = 0, 2
replacement = "Py"
changed = text[:start] + replacement + text[stop:]

For a different replacement, change replacement; the original text remains unchanged.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What a string slice counts

Python strings are sequences of Unicode code points, and a single index returns a string of length one—there is no separate character type. Slices therefore select code points, not necessarily whole user-perceived characters. A displayed character composed of a base code point and combining marks, or an emoji formed from multiple code points, may be split by a slice.

Further reading in the Python documentation

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.