What is slicing in Python?


Slicing in Python gets a sub-string from a string. The slicing range is set as parameters i.e. start, stop and step.

Syntax

Let us see the syntax

# slicing from index start to index stop-1
arr[start:stop]

# slicing from index start to the end
arr[start:]

# slicing from the beginning to index stop - 1
arr[:stop]

# slicing from the index start to index stop, by skipping step
arr[start:stop:step]

Slicing Example

In his example we will slice a string from start, end, by skipping steps, etc −

myStr = 'Hello! How are you?' print("String = ",myStr) # Slice print("Slicing from index start to index stop-1 = ",myStr[5:8]) print("Slicing from index start to the end = ",myStr[3:]) print("Slicing from the beginning to index stop - 1 = ",myStr[:5]) print("Slicing from the index start to index stop, by skipping step = ",myStr[5:11:2])

Output

String =  Hello! How are you?
Slicing from index start to index stop-1 =  ! H
Slicing from index start to the end =  lo! How are you?
Slicing from the beginning to index stop - 1 =  Hello
Slicing from the index start to index stop, by skipping step =  !Hw

Slice a string

Let us slice a string -

# Create a String myStr = 'Hello! How are you?' # Display the String print("String = ",myStr) # Slice the string print("String after slicing = ",myStr[8:11])

Output

String =  Hello! How are you?
String after slicing =  ow 

Slice a string with step

The step is used to set the increment between each index for slicing −

# Create a String myStr = 'Hello! How are you?' # Display the String print("String = ",myStr) # Slice the string with step print("String after slicing = ",myStr[8:15:2])

Output

String =  Hello! How are you?
String after slicing =  o r 

Slice a Tuple

We can slice parts of a tuple −

# Create a Tuple mytuple = ("Tim", "Mark", "Harry", "Anthony", "Forrest", "Alex", "Rock") # Display the Tuple print("Tuple = ",mytuple) # Slice the Tuple print("Tuple after slicing = ",mytuple[3:5])

Output

Tuple =  ('Tim', 'Mark', 'Harry', 'Anthony', 'Forrest', 'Alex', 'Rock')
Tuple after slicing =  ('Anthony', 'Forrest')

Slice a Tuple with step

We can slice parts of a tuple and also use the step parameter to set the increment between each index for slicing −

# Create a Tuple mytuple = ("Tim", "Mark", "Harry", "Anthony", "Forrest", "Alex", "Rock", "Paul", "David", "Steve") # Display the Tuple print("Tuple = ",mytuple) # Slice the Tuple with step 2 print("Tuple after slicing = ",mytuple[2:8:2])

Output

Tuple =  ('Tim', 'Mark', 'Harry', 'Anthony', 'Forrest', 'Alex', 'Rock', 'Paul', 'David', 'Steve')
Tuple after slicing =  ('Harry', 'Forrest', 'Rock')

Slice a List

# Create a List myList = ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] # Display the List print("List = ",myList) # Slice the List print("List after slicing = ",myList[3:6])

Output

List =  ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
List after slicing =  ['s', 't', 'u']

Slice a List with step

We can slice parts of a List and also use the step parameter to set the increment between each index for slicing -

# Create a List myList = ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] # Display the List print("List = ",myList) # Slice the List with step 2 print("List after slicing = ",myList[3:9:2])

Output

List =  ['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
List after slicing =  ['s', 'u', 'w']

Updated on: 15-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements