String Slicing in Python (original) (raw)

Last Updated : 09 Apr, 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 Python

substring = 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 Slicing

**Negative 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 Indices

Below 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])

`

Output

lmno abcdefghijkl klm hjln

**Explanation:

Reverse a String Using Slicing

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 Examples

Let’s see how to use string slicing in Python with the examples below:

Example 1: Retrieve All Characters

To 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)

`

Output

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 Position

To 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.

Python `

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 Intervals

To 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.