Substring in Python
Slicing Python Objects
Python Strings comprises of a sequence or series of characters which include special and alphanumeric characters. They are also known as arrays of characters, the only difference being that act a little different than arrays. However, Strings can be considered as arrays, most of the times.
Substring in Python language is a sequence of characters with one more string. It is also termed as ‘Slicing of String’. Python’s array functionality known as ‘slicing’ can be applied to our strings. Slicing is a broad spectrum of functionality that can be used in any array-type object.
In different programming languages, we have different functions to get the substring from the main string or source string.
Learn Python from the Basic to Advanced Level with Hands-on Training, Placements, and more with
Python Training in Bangalore
WHAT IS SUBSTRING?
For example, the substring method is used in Java to get the substring from the main string or source string. In Python, we have a couple of ways to extract a part of the string ie., a substring. There is no method like substring or substr in python. But, this can be performed using slicing and extended slicing.
Let’s look at an example of using an array
The above example clearly shows a subset of an array that is used up to 3rd element. There are 2 ‘arguments’ in Slicing which indicate the beginning and ending position of your choice in the array.
In the above example, if you wish to have only elements 2 and 3, then you must do the following:
What is slicing?
Slicing is methodology in python to access a part of data from giving sequences like string, list, tuple, etc.A slice object is created based on a set of indices. You can specify start, stop and step indices.
How to get Sub-strings by Slicing Python Strings?
As mentioned above strings and arrays can be treated equally, then similar logic can be applied to strings. Here, let’s take a look at an example which shows how this logic actually works:
Wow! You were successfully able to access the character in the same way as an element in an array.
The above example shows a “sub-string” which has come from a string. A “sub-string” can be achieved from a string just by clearly positioning your desired starting and ending points.
However, you can also choose to vomit the start and/or end position. In this way, you are indicating Python that you either wish to start a sub-string from the start or end the sub-string at the end, respectively.
Here is another example showing how to apply the above-mentioned logic:
Now, you might be wondering how it worked even when you have not specified the start or end position! Well, in this example what actually we have done is, indicated Python to get start element to end element. This method is easy and valid too. Here, you can also see that a new copy of the string or array is created. This original copy can be used for your reference and also for future modifications.
Get Placement Oriented Python Training from Industry Experts with our Python Training in Chennai
Reverse Sub-string Slicing
You can also get the reverse order of sub-string with the help of extended slicing. The below-mentioned example is just for your information, only you ever happened to apply the reverse sub-string slicing in Python.
The additional colon in the above example indicates the extended slice. The “-1” index is used to traverse the string. If you use “1” instead of “-1” you will still get the same result.
Index in python
In the python index starts from 0 in forwarding order, in reverse order index starts from -1.
Consider we have a string ‘Hello world’ stored in a variable word.
Note: String will always be enclosed in single quotes ( ‘ ‘ ) or in double quotes ( “ “ )
0 1 2 3 4 5 6 7 8 9 10
H e l l o w o r l d
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
From the above, it is clear that space is also considered an index. Not only space is considered while indexing, but also when calculating the length of a variable, but space is also considered as a character.
Phrase = "This is a phrase" len(phrase)
To calculate the length of a variable in python we have an in-built function len(). In the above example, we have calculated the length of the variable phrase. It is clear that it is having 3 spaces and while calculating the length of the variable, space is also considered as a character.
- Spaces -> 3
- Characters -> 13
- Total ->16
Now that we have seen how to calculate the length, let see how we can slice our variable.
Phrase = "This is a phrase" len(phrase) slice_ = [slice]
We have created a variable phrase and assigned ‘This is a phrase’ to that variable. Then, we have used python in-built function slice(), passed (4,5) to slice and assigned to the variable slice_.
- 4 -> start charter
- 8 -> end character – 1
In our case, the start character is 4 and the end character is 8( end – 1), so it is 7. I will start from space, which is our fourth character and end at space which is our seventh character. There is another way of slicing without using slice() function
Phrase = "This is a phrase" len(phrase) phrase[:]
'This is a phrase'
Learn Python Course to Boost your Carrer with our Python Online Training
Call the variable by its name and inside square bracket specify the start and end character. The value before colon is considered as starting character and value after colon is considered as end character (end – 1).In the above example, we have not given any start value and end value. If the start value and end value are not given, then all the characters will be displayed in the output while executing the code.
'his is a phrase'
In the above example, the start value is given but the end value is not given. In this case, it will start from the specified index and goes to the end value.
'This is a phras'
In the above example, end value is given but the start value is not given. In this case, it will start from the zeroth index and goes till the before index of given index
's is a'
In the above example, both start value and end value are given. In this case, it will start from the third index and goes till the eighth index value (end – 1)