How to use the slicing operator in Python?



The slice operator is used to slice a string. The slice() function can also be use for the same purpose. We will work around the slice operator with some examples

What is Slicing?

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] 

Let us see the syntax

Slice Operator Example

Let us see an example wherein we will slice from start to end, with step, 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 Example

In this example, we will 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 Example

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 Example

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 Example

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 Example

# 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 Example

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: 2022-09-15T11:54:39+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements