String Splicing in Python

Python strings are sequences of characters enclosed in single, double or triple quotes. Strings are immutable in python. We can access each character of a string using string splicing in python. Splicing is also termed indexing.

What is String Splicing in Python?

String splicing or indexing is a method by which we can access any character or group of characters from a python string. In python, characters or sub strings of a string can be accessed with the help of square brackets [ ] just like we access elements from a list in python. We can access a character from a string using positive indices as well as negative indices.

When using positive indices for string splicing in python, the first character of the string is given index zero and the index of subsequent characters are increased by 1 till end.

For example, we can print first character, third character and eleventh character of an string using the following program. Note that indexing is 0 based in python. i.e. first character is given the index 0 and not 1.

myString="pythonarray"
x=myString[0]
print(x)
x=myString[2]
print(x)
x=myString[10]
print(x)

Output:

P
t
e

When using negative indices for string splicing in python, the last character of the python string is given index -1 and moving backwards, each character is given index 1 less than its previous character.

In the following example, we use negative indexing for printing characters of a python string.

myString="pythonarray"
x=myString[-1]
print(x)
x=myString[-5]
print(x)
x=myString[-10]
print(x)

Output:

s
n
r

How to capture a sub string from a python string?

A sub string is a continuous part of a python string. It may start from any index and end at any index.

Using positive indexing, we can capture a sub string using square brackets [ ] operator. We can specify the index of the starting character and index of the final character of the string which is to be included in sub string. The syntax for taking out the sub string is string_name[start_index:last_index]. The character at start_index is included in the sub string but the character at last_index is not included. Only characters till index last_index-1 are included. Hence start_index is inclusive while last_index is exclusive.

In the examples given below, we can see that characters at start_index have been included in the output and characters at last_index have not been included in the output.

myString="pythonarray"
x=myString[0:5]
print(x)
x=myString[6:9]
print(x)
x=myString[0:6]
print(x)

Output:

Pytho
For
Python

To capture a sub string from starting to a given index we can leave empty the start_index value.

myString="pythonarray"
x=myString[:6]
print(x)
x=myString[:9]
print(x)
x=myString[:18]
print(x)

Output:

Python
PythonFor
pythonarray

To capture a string starting at a given index and ending at last index, we can simply leave the last_index value empty.

myString="pythonarray"
x=myString[0:]
print(x)
x=myString[6:]
print(x)
x=myString[9:]
print(x)

Output:

Pythonarray

We can also capture sub strings from python strings using negative indices in the same way as above.

myString=”Pythonarray”
x=myString[-10:-1]
print(x)
x=myString[:-1]
print(x)
x=myString[-5:-1]
print(x)

Output:

rarray
Pythonarray
nner

The sub strings in python also strings and we can perform operations like string concatenation, python string split e.t.c.

For example, we can perform string concatenation as shown in the following example.

myString="Pythonarray"
x1=myString[:6]
x2=myString[6:9]
x3=myString[9:]

x=x1+x2+x3
print(x)

Output:

Pythonarray

How to capture a sub sequence from python string?

A sub sequence of a python string is a sequence of characters which are taken out from the string without disturbing the order in which the characters are present in the string. The characters in the sub sequence may or may not be continuous characters of the input python string.

To capture a sub sequence, we use the square bracket [ ] operator. The syntax for capturing a sub sequence from a string is string_name[start_index,end_index,difference]difference denotes the number to be added to start_index to get index of next character to be included in sub sequence and difference-1 characters are skipped after a character is included in the sub sequence.

myString="Pythonarray"
x=myString[0:10:2]
print(x)
x=myString[0:10:3]
print(x)

Output:

PtoFr
Pythonarray

Conclusion

In this article, we have studied about string splicing in python. We have also seen how to capture sub strings and sub sequences from a python string using string splicing. Stay tuned for more informative articles.

Leave a Reply

Your email address will not be published. Required fields are marked *