Slicing in Python is a powerful technique used to extract a portion (substring) of a string (or any sequence like list, tuple, etc.) without modifying the original data.

It allows you to access parts of a string using index positions and supports positive and negative indexing.


🔹 Basic Syntax of Slicing

string[start : stop : step]
ParameterDescription
startIndex to begin the slice (inclusive). Default → 0.
stopIndex to stop the slice (exclusive). Default → end of string.
stepNumber of steps to move (default → 1). Used for skipping characters or reversing.

🧩 Example String

text = "PythonProgramming"
CharacterPythonProgramming
Index012345678910111213141516
Negative Index-17-16-15-14-13-12-11-10-9-8-7-6-5-4-3-2-1

🔹 1. Basic Slicing Examples

print(text[0:6])     # From index 0 to 5
print(text[6:])      # From index 6 to end
print(text[:6])      # From start to index 5
print(text[:])       # Entire string

Output:

Python
Programming
Python
PythonProgramming

✅ Notes:

  • The start index is inclusive.
  • The stop index is exclusive.
  • If you omit start or stop, Python assumes the beginning or end of the string.

🔹 2. Using Negative Indexing

Negative indices count from the end of the string (−1 = last character).

print(text[-6:])      # Last 6 characters
print(text[:-6])      # All except last 6
print(text[-11:-6])   # Slice from -11 to -7

Output:

amming
PythonPro
ogram

✅ Use negative slicing to extract parts of a string from the end.


🔹 3. Using the Step Value

The step defines how much the index increases after each iteration.

print(text[::2])     # Every 2nd character
print(text[1::3])    # Every 3rd character starting from index 1

Output:

Pto rgamn
yhrga

✅ Step value can help you skip characters or sample patterns within the string.


🔹 4. Reversing a String Using Slice

The most popular slicing trick:

print(text[::-1])     # Reverse string

Output:

gnimmargorPnohtyP

✅ Setting step = -1 reverses the direction of traversal.


🔹 5. Combining Start, Stop, and Step

print(text[0:12:2])    # From index 0 to 11, every 2nd char
print(text[2:15:3])    # From index 2 to 14, every 3rd char
print(text[-10:-2:2])  # From -10 to -3, every 2nd char

Output:

Pto rg
tPgm
ormn

🔹 6. Slicing Beyond String Length

If you specify indices beyond the string’s range, Python doesn’t throw an error — it just stops at the string’s boundary.

print(text[0:100])    # Stop > string length
print(text[-100:])    # Start < 0

Output:

PythonProgramming
PythonProgramming

✅ Slicing in Python is safe — it doesn’t raise IndexError.


🔹 7. Omitting Parameters

SliceEquivalent ToDescription
text[:]text[0:len(text)]Returns full string
text[:n]text[0:n]From start to index n−1
text[n:]text[n:len(text)]From index n to end
text[::-1]Reverse stringTraverses backward

🔹 8. Real-Life String Manipulation Examples

🧩 Extracting Substring

email = "dheeraj@example.com"
username = email[:email.index("@")]
domain = email[email.index("@")+1:]
print(username, domain)

Output:

dheeraj example.com

🧩 Get First and Last Character

word = "Python"
print(word[0], word[-1])

Output:

P n

🧩 Remove First and Last Character

print(word[1:-1])

Output:

ytho

🧩 Reverse Words in a Sentence

sentence = "Python is awesome"
reversed_sentence = sentence[::-1]
print(reversed_sentence)

Output:

emosewa si nohtyP

🔹 9. Slicing in Other Sequences

Slicing works the same way for other sequence types like lists, tuples, and byte arrays.

nums = [10, 20, 30, 40, 50, 60]
print(nums[1:4])     # [20, 30, 40]
print(nums[::-1])    # [60, 50, 40, 30, 20, 10]

🧾 Summary Table

OperationExampleResult
Slice from starttext[:6]'Python'
Slice to endtext[6:]'Programming'
Middle slicetext[0:6]'Python'
Negative slicetext[-6:]'amming'
Step slicetext[::2]'Pto rgamn'
Reversetext[::-1]'gnimmargorPnohtyP'
Safe slicingtext[0:100]Returns full string
Skip characterstext[::3]'PhPgai'

In short:

Slicing in Python lets you extract substrings efficiently using [start:stop:step].

  • Works with positive & negative indices.
  • Default slice [::] gives the full string.
  • Safe, flexible, and non-destructive.
  • Common trick: [::-1] → reverse any string instantly.

Scroll to Top