Nearby lessons
58 of 108Python - String Slicing
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
main.py
yth
Explanation: index 4 is excluded.
Word File Examples
The Word file includes several slice examples with the string durga.
main.py
rga urga urga durg durga
Different Forms of String Slicing
string[start:end]
main.py
ytho
string[start:]
main.py
thon
string[:end]
main.py
pyth
string[:]
main.py
python
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.
main.py
tho
The end index is excluded.
Using Step Value
The third slicing parameter is the step value.
Syntax: [start:end:step]
main.py
durga dra dra ug
Slice with Step Value
Python supports slicing with a step value.
Syntax: [start:end:step]
main.py
pto
Backward Direction
If the step value is negative, slicing happens in the backward direction.
main.py
agrud agd agr
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].
main.py
nohtyp
This means start from the end, move backward, and use step -1.
Negative Step Example
main.py
noht
Empty Slice and Out of Range
If slicing condition is invalid, an empty string is returned.
main.py
empty string
Unlike indexing, slicing does not raise an error for out-of-range indexes.
main.py
ython
String Operators and len()
The + operator concatenates strings and the * operator repeats strings.
The len() function returns the number of characters in a string.
main.py
durgasoft durgadurga 5
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.
main.py
user
Important Notes
- Slicing returns a substring.
- End index is excluded.
- Supports negative indexing.
- Supports step value.
- Out of range slicing does not give error.
[::-1]reverses a string.+concatenates strings.*repeats strings.len()returns the total number of characters.
Summary
| Slicing Form | Description |
|---|---|
| s[start:end] | Slice between indexes |
| s[start:] | From start index to end |
| s[:end] | From beginning to end index |
| s[:] | Complete string |
| s[::-1] | Reverse string |
| + | String concatenation |
| * | String repetition |
| len() | Total number of characters |