![]() |
VOOZH | about |
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
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 :
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).
G f G
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) :
! e o
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]
Gee ek !seGrf