Nearby lessons
63 of 159Python - String Slicing
- Understand what string slicing is and how it extracts a portion of a string
- Use the slice syntax string[start:end:step]
- Apply the slice forms string[start:end], string[start:], string[:end] and string[:]
- Slice in the forward and backward directions using step values
- Reverse a string using [::-1]
- Compare indexing with slicing and use len() and string operators
What Is String Slicing?
String slicing is used to extract a portion of a string. A slice is a piece of a string.
Slice Operator and Syntax
Python uses [] for slicing.
Syntax: string[start:end]
In slicing, the start index is included and the end index is excluded.
Meaning of Slice Parameters
| Parameter | Description |
|---|---|
| begin | Starting index (inclusive) |
| end | Ending index (exclusive) |
| step | Increment or decrement value |
Basic Slicing Example
Here the start index is included and the end index is excluded.
Word File Examples
The Word file includes several slice examples with the string durga.
Different Form 1: string[start:end]
Different Form 2: string[start:]
When the end is omitted, the slice runs to the end of the string.
Different Form 3: string[:end]
When the start is omitted, the slice begins from index 0.
Different Form 4: string[:]
When both are omitted, the whole string is returned.
Default Values in Forward Direction
When slicing in the forward direction, the default values are:
| Parameter | Default Value |
|---|---|
| begin | 0 |
| end | Length of the string |
| step | +1 |
Negative Index in Slicing
Negative indexing can also be used in slicing. The end index is excluded.
Using Step Value
The third slicing parameter is the step value.
Syntax: [start:end:step]
Backward Direction
If the step value is negative, slicing happens in the backward direction.
Default Values in Backward Direction
When slicing in the backward direction, the default values are different.
| Parameter | Default Value |
|---|---|
| begin | -1 |
| end | -(length of string + 1) |
Important: in the backward direction, if the end value is -1, the result is always empty.
- In the forward direction, if the end value is
0, the result is always empty. - Either in forward or backward direction, we can use both positive and negative index values.
Reverse String Using Slicing
We can reverse a string using [::-1]. This means start from the end, move backward, and use step -1.
Negative Step Example
Empty Slice Result
If the slicing condition is invalid, an empty string is returned.
Out of Range Slicing
Unlike indexing, slicing does not raise an error for out-of-range indexes.
String Operators and len()
The + operator concatenates strings and the * operator repeats strings.
The len() function returns the number of characters in a string.
Indexing vs Slicing
| Indexing | Slicing |
|---|---|
| Returns single character | Returns substring |
| Invalid index gives error | Out of range is handled automatically |
Real World Usage
String slicing is useful for extracting usernames, file extensions, data parsing, text processing, and reverse string operations.
- A slice extracts a part of a string using string[start:end:step]
- The start index is included and the end index is excluded
- Omitted start defaults to 0 and omitted end defaults to the string length
- A negative step slices in the backward direction and [::-1] reverses a string
- Slicing never raises an error for out-of-range indexes