Last Updated : 12 Jul, 2025
String slicing in Python is a way to get specific parts of a string by using start, end and step values. It’s especially useful for text manipulation and data parsing.
Let’s take a quick example of string slicing:
Python
s = "Hello, Python!"
print(s[0:5])
Explanation: In this example, we used the slice s[0:5] to obtain the substring "Hello" from the original string.
Syntax of String Slicing in Pythonsubstring = s[start : end : step]
Parameters:
Return Type: The result of a slicing operation is always a string (str type) that contains a subset of the characters from the original string.
Using Negative Indexing in SlicingNegative indexing is useful for accessing elements from the end of the String. The last element has an index of -1, the second last element -2 and so on.
Extract Characters Using Negative IndicesBelow example shows how to use negative numbers to access elements from the string starting from the end. Negative indexing makes it easy to get items without needing to know the exact length of the string.
Python
s = "abcdefghijklmno"
print(s[-4:])
print(s[:-3])
print(s[-5:-2])
print(s[-8:-1:2])
lmno abcdefghijkl klm hjln
Explanation:
To reverse a string, use a negative step value of -1, which moves from the end of the string to the beginning.
Python
s = "Python"
# Reverse the string
print(s[::-1])
Explanation: The slice s[::-1] starts from the end and steps backward through the string, which effectively reversing it. This method does not alter the original string.
String Slicing ExamplesLet’s see how to use string slicing in Python with the examples below:
Example 1: Retrieve All CharactersTo retrieve the entire string, use slicing without specifying any parameters.
Python
s = "Hello, World!"
# Get the entire string
s2 = s[:]
s3 = s[::]
print(s2)
print(s3)
Hello, World! Hello, World!
Explanation: Using [:] or [::] without specifying start, end or step returns the complete string.
Example 2: Get All Characters Before or After a Specific PositionTo get all the items from a specific position to the end of the string, we can specify the start index and leave the end blank.
And to get all the items before a specific index, we can specify the end index while leaving start blank.
s = "Hello, World!"
# Characters from index 7 to the end
print(s[7:])
# Characters from the start up to index 5 (exclusive)
print(s[:5])
Explanation: The slice s[7:] starts from index 7 and continues to the end of the string.
To extract characters between specific positions, provide both start and end indices.
Python
s = "Hello, World!"
# Characters from index 1 to index 5 (excluding 5)
print(s[1:5])
Explanation: The code slices the string s to extract characters starting from index 1 up to, but not including, index 5, resulting in the substring "ello".
Example 4: Get Characters at Specific IntervalsTo retrieve characters at regular intervals, use the step parameter.
Python
s = "abcdefghi"
# Every second character
print(s[::2])
# Every third character from index 1 to 8 (exclusive)
print(s[1:8:3])
Explanation: The slice s[::2] takes every second character from the string.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4