Nearby lessons

58 of 108

Python - 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
beginStarting index (inclusive)
endEnding index (exclusive)
stepIncrement or decrement value

Basic Slicing Example

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
yth

Explanation: index 4 is excluded.

Word File Examples

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
rga
urga
urga
durg
durga

Different Forms of String Slicing

string[start:end]

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
ytho

string[start:]

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
thon

string[:end]

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
pyth

string[:]

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
tho

The end index is excluded.

Using Step Value

The third slicing parameter is the step value.

Syntax: [start:end:step]

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
durga
dra
dra
ug

Slice with Step Value

Python supports slicing with a step value.

Syntax: [start:end:step]

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
pto

Backward Direction

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
nohtyp

This means start from the end, move backward, and use step -1.

Negative Step Example

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
noht

Empty Slice and Out of Range

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
empty string

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

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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
Python 3 Runtime
Loading Editor...
Output Preview
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.

🐍
main.py
Python 3 Runtime
Loading Editor...
Output Preview
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

🧠 Test Your Knowledge

4 Questions

Progress: 0 / 4
Keep Going!Python - List Collection