Open In App

How to Index and Slice Strings in Python?

Last Updated : 15 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, indexing and slicing are techniques used to access specific characters or parts of a string. Indexing means referring to an element of an iterable by its position whereas slicing is a feature that enables accessing parts of the sequence.

Indexing Strings in Python

String Indexing allows us to access individual characters in a string. Python treats strings like lists where each character is assigned an index, starting from 0. We can access characters in a String in Two ways :

  1. Accessing Characters by Positive Index Number
  2. Accessing Characters by Negative Index Number
Indexing in Python

Accessing by Positive Index Number

In this type of Indexing we pass a Positive index (which we want to access) in square brackets. The index number starts from index number 0 (which denotes the first character of a string).

python
# declaring the string 
s = "Geeks for Geeks !"

# accessing the character of str at 0th index 
print(s[0]) 

# accessing the character of str at 6th index 
print(s[6]) 

# accessing the character of str at 10th index 
print(s[10]) 

Output
G
f
G

Accessing by Negative Index Number

In this type of Indexing, we pass the Negative index(which we want to access) in square brackets. Here the index number starts from index number -1 (which denotes the last character of a string). Example 2 (Negative Indexing) : 

python
# declaring the string 
s = "Geeks for Geeks !"

# accessing the character of str at last index 
print(s[-1]) 

# accessing the character of str at 5th index from the last 
print(s[-5]) 

# accessing the character of str at 10th index from the last 
print(s[-10]) 

Output
!
e
o

Slicing Strings in Python

String Slicing allows us to extract a part of the string. We can specify a start index, end index, and step size. The general format for slicing is:

string[start : end : step]

  • start : We provide the starting index.
  • end : We provide the end index(this is not included in substring).
  • step : It is an optional argument that determines the increment between each index for slicing.
python
# declaring the string 
s ="Geeks for Geeks !"

# slicing using indexing sequence 
print(s[: 3]) 
print(s[1 : 5 : 2]) 
print(s[-1 : -12 : -2]) 

Output
Gee
ek
!seGrf

Article Tags :
Practice Tags :

Similar Reads

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy