String Slicing technique in python

A string is a data type in Python that represents a sequence of characters or text. The string in Python is immutable. This simply implies that Python strings cannot be changed after creation.

What is string slicing?

String slicing involves creating a substring from a source string. The original string remains unchanged, but the new string created is a fraction of the original string. 

Methods of slicing string in Python

There are two major ways to slice strings in Python. They include the in-built slice() function and the [:] list slice. Both methods of slicing use the index of the source string to slice it into a substring.

Indexing strings in Python:

The string can be indexed with positive or negative numbers, depending on the starting position. If you start counting from the beginning of the string, the index starts from 0 to the highest positive number, while if you start counting from the end, the indexing count starts from -1 to the highest negative value.

For example, let's declare a string as follows and index it.

string = "Textbook"

Positive indexing

  String          T         e         x         t          b           o          o         k          s
   Index         0         1          2         3           4          5           6          7          8

Negative Indexing

     String          T       e          x          t         b        o         o         k          s
      Index         -9        -8          -7          -6            -5        -4          -3        -2          -1

The slice() function:

The slice() function syntax can be represented as follows:

slice(start, end, step)

The start and step parameters are optional. The start parameter represents an integer that indicates the position to begin the slicing (the default is 0). The end parameter indicates the position to end the slicing. Finally, the step parameter determines the increment required between each index of slicing.

Now, let's demonstrate how to slice using the negative and positive indexes using the slice() function. 

string = "stringtext"
a = slice(1, 5)
print(string[a]) #trin

b = slice(0, 6, 3)
print(string[b]) #si

c = slice(-4, -1)
print(string[c]) #tex

d = slice(-1, -7, -2)
print(string[d]) #teg

Using the list slice [ : ] :

This is an easy way and convenient way to slice a string both syntax and execution-wise. It follows the same pattern as the slice() function where we have the start, end, and step. 

The syntax of slicing is represented as follows:

lists[start : end : step]

Let's slice the string below using the list slice as follows:

string="Textbooks"

print(string[0:4]) #Text
print(string[2:5]) #xtb
print(string[2:8:2])#xbo
print(string[-1:-7]) #skoobt
print(string[:]) #Textbooks
print(string[::-1]) #skoobtxeT
print(string[-1:-7:-2])#sob

Conclusion:

String slicing is defined as creating a substring from a source string. The substring is extracted from the source string using two major techniques: the slice() function and the list slice. The list slice is very convenient for slicing because of its simple syntax and execution speed.