How to slice a string in Python – Multiple ways

In this tutorial, we are going to learn how to slice a string in multiple ways in Python.

String slicing is the process of obtaining the substring of a given string with specified start & end positions.

In python, the string indexing starts from zero. The strings can also have negative indexing from the end just like the lists. The below picture shows both the negative and positive indexing of a string.

How to slice a string in Python - Multiple ways

The commonly used slicing methods are:

  • Using indexing
  • With the help of the slice() method

Using Indexing

We can slice the string using the start & stop indices within square brackets beside the string object. As a result, we get the substring of a string between the start and stop-1 indices i.e our starting index is inclusive whereas the ending index is exclusive.

Syntax:

string[start:stop:step]

start: It is the starting index of our slicing.
stop: It is the ending index of our slicing.
step: This is an optional value that specifies the increment between every index. The default value of the step is 1.

These start, stop and step indices are separated by a colon.

Since the python strings have negative indexing, we can also use negative indices in the place of start and stop positions to perform slicing.

Programs to implement string slicing

1. Using start and stop values

In the below program, we fetched the substring of the given string from 0 to 3 indices.

s="CodeSpeedy"
print(s[0:4])

Output:

Code

2. Using start, stop and step values

Here, we specified the step as 2. So the position always increments by 2 to return the substring.

s="CodeSpeedy"
print(s[0:9:2])

Output:

CdSed

3. Using negative indexing

Even though negative indices of a string are from right to left, the string only traverses from left to right. Hence we got the output as ‘Speed’

s="CodeSpeedy"
print(s[-6:-1])

Output:

Speed

4. Using a negative step value

If we use a negative step value, it means that we are traversing our string in a reverse manner i.e from right to left. So the start value must be greater than the stop value.

s="CodeSpeedy"
print(s[8:3:-1])

Output:

deepS

5. Without specifying the start and stop values

If we don’t specify the start value within brackets it takes the default value of 0. Similarly, it takes the default stop value as the length of a string. If both the start and stop values are not mentioned, it prints the whole string.

s="CodeSpeedy"
print("without start value:",s[:4])
print("without end vaue:",s[4:])
print("without start and end values:",s[:])

Output:

without start value: Code
without end vaue: Speedy
without start and end values: CodeSpeedy

Using slice() method to slice a string

In python, slice() is an inbuilt method to slice any sequences like strings, tuples and lists. Python has two types of slice() methods with different parameters.

Syntax:

  • slice(stop)
  • slice(start,stop,step)

The start, stop and step parameters are similar to the values used in our indexing method. In the first method, the start and step parameters have default values of 0 and 1 respectively.

This method returns a sliced object. To get the substring, the sliced object is used within square brackets beside our string object.

Program to implement the string slicing using the slice() method:

s="CodeSpeedy"
k1=slice(4)  #using first slice()method with only stop parameter
k2=slice(0,9,2)  #using second slice() method with start,stop and step parameters
k3=slice(-6,-1)  #using negative indexing
k4=slice(8,3,-1) #using negative step value

print(s[k1])
print(s[k2])
print(s[k3])
print(s[k4])

Output:

Code
CdSed
Speed
deepS

Also read:

Leave a Reply

Your email address will not be published.