Table of contents
1.
Introduction
2.
Indexing Strings in Python
2.1.
Accessing by Positive Index Number
2.1.1.
Example:
2.1.2.
Example:
2.2.
Accessing by Negative Index Number
2.2.1.
Example:
3.
Slicing Strings in Python
3.1.
Example: Basic Slicing
3.2.
Omitting Start and End Index
3.2.1.
Example
3.3.
Using a Step Value
3.3.1.
Example:
3.4.
Reversing a String Using Slicing
3.4.1.
Example:
3.5.
Using Negative Step Values
3.5.1.
Example:
4.
Frequently Asked Questions
4.1.
What happens if I access an index that is out of range?
4.2.
Can I use slicing on lists and tuples?
4.3.
How do I extract every third character from a string?
5.
Conclusion
Last Updated: Mar 8, 2025
Easy

Indexing and Slicing in Python

Introduction

Indexing and slicing are essential techniques in  Python used to access and manipulate sequence data types like strings, lists, and tuples. Indexing allows retrieving specific elements based on their position, while slicing enables extracting a subset of elements from a sequence. These features make data manipulation efficient and flexible. 

Indexing and Slicing in Python

In this article, you will learn about indexing, slicing, their syntax, and how to use them effectively in Python.

Indexing Strings in Python

Indexing is used to access individual characters in a string. In Python, strings are sequences of characters, and each character has a unique position, known as an index. Indexing in Python starts from 0 for the first character and moves sequentially.

Accessing by Positive Index Number

Python allows you to access characters in a string using positive index numbers. The first character is indexed as 0, the second as 1, and so on.

Example:

text = "Python"
print(text[0]) 
print(text[1]) 
print(text[5])  
You can also try this code with Online Python Compiler
Run Code

 

Output: 

P
y
n

 

Explanation:

  • text[0] fetches the first character: 'P'.
     
  • text[1] fetches the second character: 'y'.
     
  • text[5] fetches the last character: 'n'.
     

If you try to access an index that is out of range, Python will raise an IndexError.

Example:

print(text[10])  # IndexError: string index out of range

Accessing by Negative Index Number

Python also allows negative indexing, which helps when accessing elements from the end of the string. The last character is indexed as -1, the second last as -2, and so on.

Example:

text = "Python"
print(text[-1]) 
print(text[-2]) 
print(text[-6])  
You can also try this code with Online Python Compiler
Run Code

 

Output: 

n
o
P

 

Explanation:

  • text[-1] fetches the last character: 'n'.
     
  • text[-2] fetches the second last character: 'o'.
     
  • text[-6] fetches the first character: 'P'.
     

Using negative indexes prevents errors when the length of the string is unknown.

Slicing Strings in Python

Slicing is a technique used to extract a portion of a string. It allows us to specify a range and retrieve multiple characters at once. The syntax for slicing is:

string[start:end:step]

 

  • start: The index where slicing begins (inclusive).
     
  • end: The index where slicing stops (exclusive).
     
  • step: The gap between characters (optional).

Example: Basic Slicing

text = "Python Programming"
print(text[0:6])  
print(text[7:18]) 
You can also try this code with Online Python Compiler
Run Code

 

Output: 

Python
Programming

 

Explanation:

  • text[0:6] extracts characters from index 0 to 5 (not 6), resulting in 'Python'.
     
  • text[7:18] extracts characters from index 7 to 17, resulting in 'Programming'.

Omitting Start and End Index

Python allows us to omit the start and end indices to extract the entire or partial string.

Example

text = "Hello, World!"
print(text[:5]) 
print(text[7:])  
print(text[:])  
You can also try this code with Online Python Compiler
Run Code

 

Output: 

Hello
World!
Hello, World!

 

Explanation

  • text[:5] means start from index 0 and go up to index 4.
     
  • text[7:] means start from index 7 and continue till the end.
     
  • text[:] means extract the entire string.

Using a Step Value

By default, Python slices elements one by one. However, we can use the step value to skip elements.

Example:

text = "Python"
print(text[0:6:2]) 
You can also try this code with Online Python Compiler
Run Code

 

Output: 

Pto

 

Explanation:

  • text[0:6:2] picks every second character from index 0 to 5.

Reversing a String Using Slicing

One of the most common use cases of slicing is reversing a string.

Example:

text = "Python"
print(text[::-1])  
You can also try this code with Online Python Compiler
Run Code

 

Output: 

nohtyP

 

Explanation:

  • [::-1] starts from the end and moves backward, effectively reversing the string.

Using Negative Step Values

We can also use a negative step to extract characters in reverse order from a specified range.

Example:

text = "Programming"
print(text[9:2:-1])  
You can also try this code with Online Python Compiler
Run Code

 

Output: 

gnimmar

 

Explanation:

  • text[9:2:-1] extracts characters from index 9 to 3 in reverse order.

Frequently Asked Questions

What happens if I access an index that is out of range?

Python raises an IndexError if you try to access an index that does not exist in the string.

Can I use slicing on lists and tuples?

Yes, slicing works the same way for lists and tuples, allowing you to extract portions efficiently.

How do I extract every third character from a string?

You can use slicing with a step value of 3 to select every third character.

Conclusion

In this article, we learned indexing and slicing in  Python, which are essential techniques for accessing and manipulating sequences like strings, lists, and tuples. Indexing helps retrieve specific elements, while slicing allows extracting subparts of a sequence using a defined range. Mastering these concepts improves efficiency in data handling, string processing, and algorithm development in  Python.

Live masterclass