Data Scientist | AI Practitioner | Software Developer. Giving talks, teaching, writing.
In python, a String is a sequence of characters that may contain special characters or alphanumeric characters, for example, "we meet on Friday at 08:00 am". It is possible to access sub-parts of the string commonly known as a python substring.
You can also define substring as a sequence of characters within a string. From the previous example, python substrings can be "Friday", "at", "meet", and others.
Python provides different ways and methods to generate a substring, check if a substring is present, index of a substring, and more.
You can extract a substring from a string by slicing with indices that obtain your substring as follows:
Indices can be positive or negative numbers. The positive indices start from the beginning to the end of the string and the negative indices start from the end to the beginning of a string.
In this article, you will learn various operations related to substrings in python.
This example will show you how to slice the first 5 characters from the string.
string = "hello world" print(string[:5])
Here you define the stop index which is 5. The start index is by default is 0.
This example will show you how to slice characters from index 3 to index 5 from the string.
string = "hello world" print(string[3:5])
To get the last character use the -1 index (negative index). Check the following example.
string = "hacker noon" print(string[-1])
In this example, you will slice the last 4 characters from the string. Here you use the negative index to start slicing from the end of the string.
string = "hackernoon" print(string[-4:])
You can slice the string with steps after indicating start-index and stop-index. By default, the step is 1 but in the following example, the step size is 2.
string = "welcome to hackernoon" print(string[::2])
Sometimes you want to check if a substring present in a string. The following example will validate if a substring ‘noon’ is in a string.
substring = "noon" string = "welcome to hackernoon" print(substring in string)
If present, it will return True otherwise False.
You can use the find() method to check if a substring present in a string.
Let’s check the following example.
substring = "zz" string = "hello world" print(string.find(substring))
If available it returns the left-most index of the substring otherwise return -1 means not available.
This means “zz” is not present in “hello world”.
You can choose to slice a specific character according to its index number.
string ="hello world" print(string)
You can use the split() method to create a list of substrings. Let’s check the following example.
string = "welcome to hackernoon platform" print(string.split())
['welcome', 'to', 'hackernoon', 'platform']
To reverse the string, the step must be a negative value. Example -1.
string = "welcome to hackernoon" print(string[::-1])
‘noonrekcah ot emoclew’
You can use the count() method to know how many times a particular substring presented in a string.
string = "we will have a quick introduction to hackernoon this afternoon" print(string.count('noon'))
Congratulations 👏👏, you have made it to the end of this article! I hope you have learned something new about python substring.
If you learned something new or enjoyed reading this article, please share it so that others can see it. Until then, see you in the next post!
You can also find me on Twitter @Davis_McDavid.
And you can read more articles like this here.
Want to keep up to date with all the latest in python? Subscribe to our newsletter in the footer below!
Create your free account to unlock your custom reading experience.