Nearby lessons

63 of 159

Python - String Slicing

📌 What You Will Learn
  • 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
beginStarting index (inclusive)
endEnding index (exclusive)
stepIncrement or decrement value

Basic Slicing Example

Here the start index is included and the end index is excluded.

🐍Code Cell
1s = "python"
2print(s[1:4])
Output
yth

Word File Examples

The Word file includes several slice examples with the string durga.

🐍Code Cell
1s = "durga"
2print(s[2:100])
3print(s[1:40])
4print(s[1:])
5print(s[:4])
6print(s[:])
Output
rga
urga
urga
durg
durga

Different Form 1: string[start:end]

🐍Code Cell
1s = "python"
2print(s[1:5])
Output
ytho

Different Form 2: string[start:]

When the end is omitted, the slice runs to the end of the string.

🐍Code Cell
1s = "python"
2print(s[2:])
Output
thon

Different Form 3: string[:end]

When the start is omitted, the slice begins from index 0.

🐍Code Cell
1s = "python"
2print(s[:4])
Output
pyth

Different Form 4: string[:]

When both are omitted, the whole string is returned.

🐍Code Cell
1s = "python"
2print(s[:])
Output
python

Default Values in Forward Direction

When slicing in the forward direction, the default values are:

Parameter Default Value
begin0
endLength of the string
step+1

Negative Index in Slicing

Negative indexing can also be used in slicing. The end index is excluded.

🐍Code Cell
1s = "python"
2print(s[-4:-1])
Output
tho

Using Step Value

The third slicing parameter is the step value.

Syntax: [start:end:step]

🐍Code Cell
1s = "durga"
2print(s[0:5:1])
3print(s[0:5:2])
4print(s[::2])
5print(s[1::2])
Output
durga
dra
dra
ug

Backward Direction

If the step value is negative, slicing happens in the backward direction.

🐍Code Cell
1s = "durga"
2print(s[::-1])
3print(s[::-2])
4print(s[4:1:-1])
Output
agrud
ard
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]. This means start from the end, move backward, and use step -1.

🐍Code Cell
1s = "python"
2print(s[::-1])
Output
nohtyp

Negative Step Example

🐍Code Cell
1s = "python"
2print(s[5:1:-1])
Output
noht

Empty Slice Result

If the slicing condition is invalid, an empty string is returned.

🐍Code Cell
1s = "python"
2print(s[2:1])
Output
No output captured.

Out of Range Slicing

Unlike indexing, slicing does not raise an error for out-of-range indexes.

🐍Code Cell
1s = "python"
2print(s[1:100])
Output
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.

🐍Code Cell
1print("durga" + "soft")
2print("durga" * 2)
3 
4s = "durga"
5print(len(s))
Output
durgasoft
durgadurga
5

Indexing vs Slicing

Indexing Slicing
Returns single characterReturns substring
Invalid index gives errorOut 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.

🐍Code Cell
1email = "user@gmail.com"
2print(email[:4])
Output
user
📝 Key Takeaways
  • 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

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4