String Accessing in Python
Introduction
In Python, a string is a sequence of characters enclosed within single quotes (' '), double quotes (" "), or triple quotes (''' ''' or """ """). Strings are widely used to store and manipulate text such as names, messages, sentences, and other textual data.
String accessing refers to the process of accessing individual characters or a group of characters from a string. Python provides simple and powerful ways to access string elements using indexing and slicing.
Since strings are ordered sequences, each character has a specific position called an index. Python supports both positive indexing, which starts from 0 and moves from left to right, and negative indexing, which starts from -1 and moves from right to left.
For example:
text = "Python"The characters can be accessed as:
P y t h o n
0 1 2 3 4 5 → Positive Index
-6 -5 -4 -3 -2 -1 → Negative IndexPython mainly provides the following ways to access strings:
1. String Indexing
Indexing is used to access a single character from a string using its position.
text = "Python"
print(text[0])Output:
P2. Negative Indexing
Negative indexing allows us to access characters starting from the end of the string.
text = "Python"
print(text[-1])Output:
n3. String Slicing
Slicing is used to access a part or sequence of characters from a string.
text = "Python"
print(text[0:3])Output:
PytThe general syntax for slicing is:
string[start:stop:step]Here, start specifies where the slicing begins, stop specifies where it ends (the stop index is not included), and step specifies the number of positions to move at a time.
Conclusion
String accessing is an important concept in Python because it allows developers to retrieve individual characters or specific portions of text efficiently. Indexing, negative indexing, and slicing are commonly used techniques for working with strings in Python.
4. Mixed Slicing
Mixed slicing refers to using different combinations of positive and negative indexes while slicing a string. It is useful when we want to access a specific portion of a string by combining indexes from the beginning and the end.
Get Devikumpatla’s stories in your inbox
Join Medium for free to get updates from this writer.
The general syntax is:
string[start:stop:step]Python allows us to use positive and negative values for start, stop, and step.
Example 1: Positive Start and Negative Stop
text = "Python Programming"print(text[0:-7])Output:
Python ProgHere, 0 starts from the beginning, while -7 refers to a position counted from the end.
Example 2: Negative Start and Positive Stop
text = "Python"print(text[-6:5])Output:
PythoThe slicing starts with a negative index and stops at a positive index.
Example 3: Negative Start and Negative Stop
text = "Python Programming"print(text[-11:-7])Output:
ProgBoth the starting and ending positions are counted from the end of the string.
Example 4: Using Step with Mixed Indexes
text = "Python Programming"print(text[0:-1:2])Output:
Pto rgamnHere:
0→ starts from the beginning-1→ stops before the last character2→ skips every alternate character
Important Point
Mixed slicing gives us flexibility to access different parts of a string by combining positive indexing, negative indexing, and step values.
For example:
text = "PYTHON"print(text[1:-1])Output:
YTHOThis starts from index 1 and stops before index -1.
Note: In Python slicing, the stop index is always excluded.





