Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How does string slicing work in Python?

Python
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 27, 2026
5 min read

Python string slicing extracts part of a string with the syntax text[start:stop:step]. The slice begins at index start, stops before index stop, and moves by step characters. Any of the three parts can be left out, and the result is always a new string.

Slicing works the same way on lists, tuples and other sequences, so learning it for strings pays off everywhere. The examples use Python 3.13 and 3.14.

Table of Contents

  • How indexes work in a string
  • The start, stop and step syntax
  • Negative indexes and reversing a string
  • Slices never raise IndexError
  • Slices create new strings
  • The slice object and practical examples
  • Frequently Asked Questions
    • Why is the stop index excluded in a Python slice?
    • What does text[::-1] do?
    • Can you change a string by assigning to a slice?
    • What happens if the slice indexes are out of range?
  • Summary

How indexes work in a string

Every character in a string has a position, called an index. Counting starts at 0 from the left. Negative numbers count from the right, where -1 is the last character.

text
python
text

Reading a single character with an index outside the string raises IndexError. Slices behave differently, as shown below.

The start, stop and step syntax

The basic form is text[start:stop]. The start index is included and the stop index is excluded.

python
text

Leaving out start means the beginning, and leaving out stop means the end. A useful rule is that text[:n] gives the first n characters, and the length of text[a:b] is b - a when both are inside the string.

The third value is the step, which sets how far to move each time:

python
text

Negative indexes and reversing a string

Negative values are useful when you care about the end of a string:

python
text

A negative step walks backwards. The common idiom for reversing a string is a step of -1:

python
text

With a negative step the defaults swap, so start defaults to the end and stop to the beginning. For example, "abcdef"[4:1:-1] returns "edc".

Slices never raise IndexError

An out-of-range slice is clipped to the string rather than failing:

python
text

The second print shows an empty line, because the result is an empty string. This makes slices safe for tasks like taking "at most the first 80 characters" with text[:80]. A step of 0 is the exception and raises ValueError.

Slices create new strings

Strings are immutable, so you cannot assign into a slice:

python
text

Build a new string instead:

python
text

Each slice copies the selected characters into a new string. For very large strings, repeated slicing in a loop can be expensive, so consider iterating or using str.find() and str.partition() when that matters.

The slice object and practical examples

The syntax text[a:b:c] is shorthand for text[slice(a, b, c)]. Naming a slice makes fixed-width parsing readable:

python
text

More everyday uses:

TaskCodeResult for "hello world"
First word if length is knowns[:5]hello
Last three characterss[-3:]rld
Every other characters[::2]hlowrd
Drop first and lasts[1:-1]ello worl
Check palindromes == s[::-1]False

Slicing counts characters (Unicode code points), not bytes, so "héllo"[1] is "é". Some visible characters, such as emoji with modifiers, are made of several code points and can be split by a slice.

Frequently Asked Questions

Why is the stop index excluded in a Python slice?

Half-open ranges make lengths easy to calculate and let slices join cleanly, for example s[:3] + s[3:] is always the original string.

What does text[::-1] do?

It returns the string reversed, because the step is -1 and both start and stop are left at their defaults.

Can you change a string by assigning to a slice?

No. Strings are immutable. Create a new string by joining slices with other text, or convert to a list of characters, change it and join it back.

What happens if the slice indexes are out of range?

Python clips them and returns whatever overlaps, possibly an empty string. Only a single-index lookup such as text[10] raises IndexError.

Summary

Use text[start:stop:step] to take part of a string: start is included, stop is excluded, and negative values count from the end. Omitted parts use sensible defaults, out-of-range slices are clipped, and every slice returns a new string.

Share

Similar posts

  • How do you format numbers with f-strings in Python?
  • How do you use f-strings in Python?
  • What are escape sequences in Python strings?
  • Why are strings immutable in Python?
  • How do you check if a string is a number in Python?
  • How do you count occurrences of a substring in Python?
  • How do you check if a string contains a substring in Python?
  • How do you change the case of a string in Python?