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.

Loading editor…

Word File Examples

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

Loading editor…

Different Form 1: string[start:end]

Loading editor…

Different Form 2: string[start:]

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

Loading editor…

Different Form 3: string[:end]

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

Loading editor…

Different Form 4: string[:]

When both are omitted, the whole string is returned.

Loading editor…

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.

Loading editor…

Using Step Value

The third slicing parameter is the step value.

Syntax: [start:end:step]

Loading editor…

Backward Direction

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

Loading editor…

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.

Loading editor…

Negative Step Example

Loading editor…

Empty Slice Result

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

Loading editor…

Out of Range Slicing

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

Loading editor…

String Operators and len()

The + operator concatenates strings and the * operator repeats strings.

The len() function returns the number of characters in a string.

Loading editor…

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.

Loading editor…
📝 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

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.