How to Index and Slice Strings in Python?
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.
Table of Content
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 :
- Accessing Characters by Positive Index Number
- 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).
# 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) :
# 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.
# 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