← Back to Chapters

Python String Slicing

✂️ Python String Slicing

ℹ️ Quick Overview

String slicing in Python lets you extract a part (substring) from a string using the syntax string[start:end] or string[start:end:step]. The start index is included, but the end index is excluded. You can also use negative indexes to count from the end of the string and a step value to skip characters or even reverse the string.

? Key Concepts

  • Indexing starts at 0 – the first character has index 0.
  • Start index – where the slice begins (included).
  • End index – where the slice stops (excluded).
  • Step – how many characters to skip each time (default is 1).
  • Negative indexes – count from the end (last character is -1).
  • Omitting start or end – Python assumes the beginning or end of the string.
  • Slicing returns a new string – the original string is not changed.

? Syntax and Theory

The general slicing syntax in Python is:

? View Slicing Syntax
string[start:end]
string[start:end:step]

# start  → index to begin from (included)
# end    → index to stop at (excluded)
# step   → jump size (default = 1)

If start is omitted, Python starts from the beginning of the string. If end is omitted, Python slices until the end. If step is negative, the slice goes in reverse.

? Code Examples

? Basic Slicing

Extract different parts of a string using start and end indexes.

? View Basic Slicing Example
text = "PythonProgramming"
print(text[0:6])   # Python
print(text[6:])    # Programming
print(text[:6])    # Python

? Slicing with Step

Use the step value to skip characters while slicing.

? View Step Slicing Example
text = "PythonProgramming"
print(text[0:16:2])   # Pto rgamn
print(text[::3])      # Phgai

? Negative Indexing

Negative indexes let you slice from the end of the string.

? View Negative Indexing Example
text = "PythonProgramming"
print(text[-1])      # g
print(text[-7:-1])   # ramming
print(text[:-7])     # PythonProg

? Reversing a String

Use a negative step to reverse the entire string.

? View Reverse String Example
text = "Python"
print(text[::-1])   # nohtyP

? Combining Start, End, and Step

Combine all three parameters to get specific patterns from a string.

? View Combined Slicing Example
text = "PythonProgramming"
print(text[1:12:2])   # yhnPorm

? Output and Explanation

  • text[0:6] → characters from index 0 to 5"Python".
  • text[6:] → from index 6 to the end → "Programming".
  • text[:6] → from start until index 6 (excluded) → "Python".
  • text[0:16:2] → every 2nd character from index 0 to 15.
  • text[::3] → every 3rd character from the entire string.
  • text[-7:-1] → 7th character from the end up to (but not including) the last character.
  • text[::-1] → full string reversed.
  • text[1:12:2] → from index 1 to 11, taking every 2nd character.

Notice how the end index is never included in the slice, and how a negative step reverses the direction of slicing.

? Interactive Slicing Example

Leave blank for default step 1.

? Result

Result will appear here after you click Run Slice.

? Tips and Best Practices

  • Use [::] to create a full copy of a string.
  • Combine negative indexing with a negative step to reverse specific parts of a string.
  • Remember that the end index is excluded – this helps avoid off-by-one errors.
  • Use slicing instead of loops when you just need a substring – it is cleaner and more Pythonic.
  • Test your slices on short sample strings to understand exactly which characters are included.

? Try It Yourself

  • Extract the word "Programming" from "PythonProgramming" using slicing.
  • Reverse the string "DataScience" using slicing.
  • Print every 2nd character from "MachineLearning" using a step value.
  • Use a combination of positive and negative slicing to get the middle part of a long string.
  • Create your own string and experiment with different start, end, and step values.