In Python (programming language), string slicing is used to extract a part of a string using index positions.

A string is a sequence of characters, and each character has an index.

Example string:

P  y  t  h  o  n
0 1 2 3 4 5

1️⃣ Basic Slicing Syntax

string[start : end]
  • start β†’ starting index
  • end β†’ ending index (not included)

Example:

text = "Python"
print(text[0:4])

Output

Pyth

Explanation
Index 0 to 3 is selected (4 is excluded).


2️⃣ Slice From Beginning

If the start index is omitted, Python starts from the beginning.

text = "Python"
print(text[:4])

Output

Pyth

3️⃣ Slice to the End

If the end index is omitted, Python slices to the end of the string.

text = "Python"
print(text[2:])

Output

thon

4️⃣ Negative Indexing

Python allows slicing from the end of the string using negative indices.

P  y  t  h  o  n
-6 -5 -4 -3 -2 -1

Example:

text = "Python"
print(text[-4:-1])

Output

tho

5️⃣ Slicing with Step

Syntax:

string[start:end:step]

Example:

text = "Python"
print(text[0:6:2])

Output

Pto

Explanation
Step = 2 β†’ skip one character.


6️⃣ Reverse a String

Use a negative step.

text = "Python"

print(text[::-1])

Output

nohtyP

7️⃣ Get Every Second Character

text = "Programming"

print(text[::2])

Output

Pormig

⭐ Example Program

text = "Python Programming"
print(text[0:6]) # Python
print(text[7:]) # Programming
print(text[::-1]) # Reverse string

Output

Python
Programming
gnimmargorP nohtyP

πŸ“Š String Slicing Summary

SyntaxMeaning
text[start:end]Slice between indexes
text[:end]From start to index
text[start:]From index to end
text[::step]Skip characters
text[::-1]Reverse string

βœ… Key Points

  • Strings are indexed starting from 0
  • End index is not included
  • Negative indices access characters from the end