What is String Slicing in Python?

A string is the sequence of specific characters including names of people, things, and places. It is the most common data type in Python that supports various operations, such as indexing, slicing, concatenation, etc. Strings cannot be modified/changed once they are defined. However, users can create new strings from existing ones through various methods. While dealing with larger or complex strings, users may need to extract a specific part of the string or reverse the string. In this situation, they can do a string slicing. 

This blog will illustrate Python string slicing. 

Quick Outline 

  1. What is Python String Slicing?
  2. How to do String Slicing in Python?

What is Python String Slicing?

String slicing is a way of creating and getting a substring from the provided larger string by slicing it. It permits users to access and manage/manipulate the desired parts of a particular string. It is also utilized in the case of reversing a string. 

In Python, each string character has a unique specified index that starts from “0”. This means that the string’s first character is placed at index “0”, the second character is located at index “1”, and so forth. There is also negative indexing in Python that starts from index “-1”. This indicates that the string’s last character is placed at index “-1”, the second last character is located at “-2”, and so on.

Index Tracker for Positive and Negative Indices

Look at the following figure to understand the concept of positive and negative indexing. 

In this figure, there is a string “ItsLinuxFoss” marked with the indices. The top indices denote the positive indices and the bottom indices denote the negative indices. 

How to do String Slicing in Python?

There are different possible ways to slice a particular string in Python. Users can either utilize the built-in “slice()” function or use the array slicing method. We have listed down these two approaches that are useful for string slicing in Python. Check out each approach and see how they work:

  • Approach 1: Slicing a String Using the “slice()” Method
  • Approach 2: Slicing a String Using the Array Slicing [::] Method

Approach 1: Slicing a String Using the “slice()” Method

The “slice()” is a Python built-in function that creates a slice object for specific slicing and returns the sliced string (substring). It breaks the string according to the specified range parameters.

Syntax

The syntax of the “slice()” method is:

slice(start, stop, step)

Note: Replace “start”, “stop” and “step” with their desired numeric values.

Parameter

The “slice()” method can accept/take up to three parameters. These parameters include start, stop, and step.

  • Start is an optional parameter. It is a starting value where the string’s slicing begins.
  • Stop is the essential parameter that must be specified. It is the ending index (value) where the string’s slicing stops. 
  • Step is also an optional parameter. It provides the slicing increment between each index. 

Return Value

The “slice()” method returns a sliced object that has elements in the specified range. 

Go through the provided examples to understand how to slice a string using the “slice()” method through different parameters. 

Example 1: Using the “slice()” Method With the Stop Parameter

In this example, we will slice a string using the “slice()” method by passing it only the (stop) parameter i.e. “5”. This will slice a string up to index “5” and return the characters from index 0 to index 4:

String = 'ItsLinuxFoss'

s1 = slice(5)

print(String[s1])

The below output displays the sliced string:

Moreover, users can also specify the negative value as a stop parameter to slice the string apart from the end characters. Here we have specified a stop parameter “-3” to get a substring by removing the last three characters:

String = 'ItsLinuxFoss'

s2 = slice(-3)

print(String[s2])

The “slice()” method has successfully retrieved a substring by omitting the specified characters:

Example 2: Using the “slice()” Method With the Start and Stop Parameters

In this example, we will define the “slice()” method with the start and stop parameters to slice a string. We have defined the start parameter as “2” and the stop parameter as “5”. This indicates that the string will be sliced from the index 2 to index 4:

String = 'ItsLinuxFoss'

s3 = slice(2, 5)

print(String[s3])

It can be observed that the string has been sliced according to the specified parameters:

Furthermore, users can also specify the negative values as a start and stop parameter to start slicing a string from the end:

String = 'ItsLinuxFoss'

s4 = slice(-9, -2)

print(String[s4])

The below output shows the sliced string from index -9 to -3 (excluded index -2):

Moreover, both positive and negative indexes can also be defined at the same time in the “slice()” function as seen below:

String = 'ItsLinuxFoss'

s5 = slice(4, -2)

print(String[s5])

The “slice()” method has successfully fetched the substring based on the specified indexes i.e. index 4 to index -3:

Example 3: Using the “slice()” Method With the Start, Stop and Step Parameter

We will slice a string by utilizing all three parameters in the “slice()” method. We have defined the start parameter as “2”, the stop parameter as “9”, and the step parameter as “2”. This means that the string should be sliced from index 2 and end at index 8 with the step value or increment of 2”:

String = 'ItsLinuxFoss'

s6 = slice(2, 9, 2)

print(String[s6])

It can be seen that the string has been sliced according to the defined parameters:

Users can also define negative values to all the parameters in the “slice()” function to start slicing from the end of the string as seen below:

String = 'ItsLinuxFoss'

s7 = slice(-3, -12, -2)

print(String[s7])

The “slice()” method with negative values as parameters has sliced a string in reversed order from index -3 to index -12 with the step value of -2”:

Approach 2: Slicing a String Using the Array Slicing [::] Method

The array slicing method is the simplest method to fetch the substring from a desired string using the slice operator “[::]”. This method directly slices the specific string and returns the substring (sliced string).

Syntax

The syntax of the array slicing method is:

String_object[start:stop:step]

Note: Replace “start”, “stop” and “step” with their desired numeric values.

Parameter

The array slicing method accepts up to three parameters. These parameters include start, stop, and step.

  • Start is the starting index (value) where the string’s slicing starts.
  • Stop is the ending index (value) where the string’s slicing stops. 
  • Step defines the slicing increment between each index.  

Go through the following examples to see how to slice a string using the array slicing method with different parameters. 

Example 1: Using the Array Slicing [::] Method With the Start Parameter

In this example, we will use only the start parameter i.e. “6” in the array slicing method to slice a string. In this case, the stop parameter is “0” and the step parameter is set to “1” by default. This means it will start slicing a string from index 6 up to the end:

String = 'ItsLinuxFoss'

print(String[6:])

The array slicing method has returned the following substring:

Users can also specify the negative value to the start parameter to slice a string from the last: 

String = 'ItsLinuxFoss'

print(String[-5:])

By doing so, the string will be sliced from the specified index i.e. “-5”:

Example 2: Using the Array Slicing [::] Method With the Stop Parameter

Here, we will slice the string using the stop parameter i.e. “5”. This means the slicing will begin from index 0 and stop at index 4:

String = 'ItsLinuxFoss'
print(String[:5])

The below output shows the sliced string:

We can also pass the negative value as a stop argument to exclude the string characters from the end:

String = 'ItsLinuxFoss'

print(String[:-3])

The array slicing method has sliced the string by removing the last three (3) characters:

Example 3: Using the Array Slicing [::] Method With the Start and Stop Parameters

Now, we will slice the string with both start and stop parameters in the array slicing method. Here, we have specified the start parameter as “2” and the end parameter as “7”. This will create the substring from index 2 to index 6:

String = 'ItsLinuxFoss'

print(String[2:7])

The below output indicates that the list has been sliced according to the specified parameters:

Furthermore, users can also provide the negative values to both start and stop parameters to start slicing a string from the end:

String = 'ItsLinuxFoss'

print(String[-8:-2])

Upon doing so, the string will be sliced based on the specified parameters i.e. from index -8 to -3 (excluded index -2):

We can also define both positive and negative indexes at the same time in the array-slicing method as seen below:

String = 'ItsLinuxFoss'

print(String[6:-3])

The below output displays the substring from index 6 to index -4:

Example 4: Using the Array Slicing [::] Method With the Step Parameter

We will utilize the array slicing method with only step parameter i.e. “2”. This means that the string should be sliced from index 0 to the end with the step value 2:

String = 'ItsLinuxFoss'

print(String[::2])

Using the array slicing method with step parameter “2” gives the following output:

Moreover, we can also reverse a particular string by specifying the step value “-1” as seen below:

String = 'ItsLinuxFoss'

print(String[::-1])

It can be observed that the string has been reversed successfully:

Example 5: Using the Array Slicing [::] Method With the Start, Stop and Step Parameters

Here, we will use all the parameters together to slice a desired string. The start parameter is set to “2”, the stop parameter is “11” and the step size is “2”. This means that the string should be sliced from index 2 and end at index 11 with the step value 2:

String = 'ItsLinuxFoss'

print(String[2:11:2])

The below output indicates that the string has been sliced according to the defined parameters:

Now, we will slice the string with the negative step size value i.e. “-2”:

String = 'ItsLinuxFoss'

print(String[7:1:-2])

By doing so, the substring has been returned in reverse order from index 7 to 2 (excluded index 1) with step size 2:

Moreover, users can also specify negative values to all the parameters in the array indexing method as seen below:

String = 'ItsLinuxFoss'

print(String[-3:-9:-2])

The array slicing method with negative parameter values returns the following output:

That was all about string slicing in Python.

Note: To access our Google Colab Notebook, click on the provided link.

Final Thoughts

String slicing is a method of accessing a substring from a large or complex string. It permits users to extract a specific part/portion of a desired string by specifying the required start or stop parameters. It can also be used to reverse strings in Python. To slice a string in Python, users can either use the built-in “slice()” function or the array slicing method. This blog has explained string slicing and the methods of slicing a string in Python.