Substrings in Python are the subset of strings. But first, what is a String? A string is a sequence that can include characters like numbers, letters, or symbols.
- Characters in a string have their own unique indexes, which helps in accessing them.
- Index value starts with 0 in forwarding order and from -1. in reverse order.
Consider we have a string “CODING NINJA”
From this example, you can conclude that space is also considered as a character in a string.
We can get the length of this string using the built-in len() method.len(CODING NINJA) will give output as 12 because there are 12 characters in this string.
What is a Substring?
A substring is a portion of text taken from a string, or we can say that substring is a subset of a string. In Python, it is also referred to as the slicing of string.
Why do we need substrings?
Suppose you are having the full name of your friend in a string. Now you want to store only your first name. Take a minute to think about how you are going to do it in the Python programming language?
That’s where python substring or slicing is going to help. To get a specific part of a string known as substring, you will use a technique called slicing in Python.
Extracting Python Substrings using slice() constructor
Python offers many ways to provide substrings of a string. One of them is ‘slicing.’
Slicing follows this template:
|string[start: end: step]|
- Indicates the starting index of the python substring.
- Substring includes the character present at this index.
- Start is assumed to be 0 if it’s not provided.
- This refers to the terminating index of the python substring.
- Substring does not include the character present at this index.
- In case the end value is not provided, or it exceeds the string length, then it is presumed to be the same as the length of a string.
- slice will ignore every Nth character.
- By default, the step is equal to 1.
Note: The start or end index can also take a negative number. A negative index means that you started counting from the end of the string (i.e., from right to left).
Now we have understood a lot about python substrings so let’s play with them using code. Given a string: –
ourString = “Code Studio!”
#Find character at index position 2 print(ourString) Output: d #Find character at index position -1 print(ourString[-1]) Output: ! #Find character in range print(ourString[1:6]) Output: ode S #Find characters before index value 6 print(ourString[:6]) Output: Code S #Find characters after index value 6 print(ourString[6:]) Output: tudio! # Find every second character between the index range of 0-12 print(ourString[0:12:2]) Output: Cd tdo
Substring Using For Loop
You can use a for loop with a range function to return a python substring. For this, you have to use the print function along with the end argument. The below example will make this clear to you.
string = 'Coding Ninjas Courses' for n in range(3, 18): print(string[n], end = '') Output: ing Ninjas Cour
Check Presence of your Python Substring in a String
There are many ways to check if the given string contains the substring you are looking for. Let’s see them one by one:
- We can check the presence of a substring in a given string using the Python If Else and not In operator. Let’s see with an example:
string = 'Practise Awesome Questions On CodeStudio' substring = 'CodeStudio' if substring not in string: print('We haven\'t found what you are looking for = ', substring) else: print('We found the Matching substring = ', substring) Output: We found the Matching substring = CodeStudio
- Python find function: We can use the built-in find function in Python to check whether a given python substring is present in the string.
string = 'Coding Ninjas Offer Variety Of Courses' substring = 'abc' if string.find(substring) == -1: print('We haven\'t found what you are looking for = ', substring) else: print('We found the Matching substring = ', substring) Output: We haven't found what you are looking for = abc
- Index of Substring: You can find the index of python substring or character in a given string using the python built-in index function. index() method returns the first occurrence of python substring. In case a python substring is not found, it raises an exception.
Syntax Of Index Method:
s.index(sub[, start[, end]] )
sub: This refers to substring that has to be searched in the string str.
start and end(optional): substring is searched within s[start:end].
Now the question arises that find() is also doing the same work, then what is the need for index() function in Python? Good Question!
So the answer to your question is that the find() method returns -1 if the substring is not found, whereas index() throws an exception. To learn more about exception handling in python, read this.
Python Substring Before Character
To get a substring present before some character, we can use the built-in string function called Python Split Function. The below example will make this more clear to you:
string = 'Coding Ninjas' first, second = string.split(' ') print('Substring Before Character space = ', first) Output: Substring Before Character space = Coding
In the above example, we divided the string into two parts using space as the character, and then we stored those divided strings. This way, we can get the string before any character.
Python Substring After Character
To get a substring that is present after a character, you can use this built-in string function called Python Split Function.
Now let’s see the usage of these . Suppose you want to store a substring before @ symbol and domain name as a separate string, then you can do it easily using the above concepts.
string = 'email@example.com' name, domain = string.split('@') print('Substring Before Character @ = ', name) print('Substring After Character @ = ', domain) Output: Substring Before Character @ = students Substring After Character @ = codingninjas.com
From this example, you can conclude that to obtain a string present before or after a character, you first have to split a string using a built-in split function like here, you have divided the string on the basis of @.
Python string count()
Python offers a built-in count() function with which you can count occurrences of a python substring in the given string.
syntax of count() method:
string.count(substring, start=..., end=...)
- substring: This refers to a string whose count is to be found.
- start (Optional): starting index within the string where the search starts.
- end (Optional): ending index within the string where the search ends.
Retrieve All Substrings from a String In Python
We can get all substrings of a string in Python using list comprehension. Let’s see this with the help of an example.
s= 'Code Studio' str=[s[a: b] for a in range(len(s)) for b in range(i + 1, len(s) + 1)] print (str) Output: ['C', 'Co', 'Cod', 'Codi', 'Codin', 'Coding', 'o', 'od', 'odi', 'odin', 'oding', 'd', 'di', 'din', 'ding', 'i', 'in', 'ing', 'n', 'ng', 'g']
From the above example, it is clear that we can get all the python substrings irrespective of their length using a for loop in Python. Now let us try this question to understand better.
Pattern matching In Python Using Regex
Let’s understand regex first, Special sequences of characters that will help you match strings are known as Regular expressions or regexes.
- With the help of Regex, we can specify a pattern of text to search.
- Python contains all the regex functions in the re module.
- This module defines several functions and constants to work with RegEx like:
- re.findall(): This method returns a list of strings containing all matches.
import re string = 'Practice 10 questions daily on 5 topics’ pattern = '\d+' result = re.findall(pattern, string) print(result) Output: ['10', '5']
- re.search(): This method looks for the first location where the RegEx pattern produces a match with the string. re.search() returns a match object, If the search is successful otherwise it returns None. It will become more clear with the below example:
import re string = "Coding is Fun with Coding Ninjas" # check if 'Coding' is at the beginning match = re.search('\ACoding', string) if match: print("pattern found inside the string") else: print("pattern not found") Output: pattern found inside the string
Frequently Asked Questions
Let’s see the count of substrings of different lengths,
Substrings of length 1:- N substrings are possible.
Substrings of length 2:- N-1 substrings are possible.
Substrings of length N:- 1 substring is possible.
To get total substrings count we have to sum up all these i.e
N+(N-1)+(N-2)+(N-3)…………..+1.So we have got the final answer to our question i.e. N*(N+1)/2 substrings.
Regular expressions in Python are supported by the re module of the standard library and you can import it by writing import re.
A Sequence or a word that remains the same either we read it from left to right or right to left. Few examples are madam, 1221, 2222 etc.
Now let’s try a problem at this concept: Given a string of length N we have to find the largest palindromic substring.
We can find a pattern present at any position within the string with the help of re.search() function.
You can use find() method or the in operator in Python to check whether the given substring is present in the string .-1 is returned by the find() method if the substring is not found.
In a nutshell, we can say that String is a sequence of characters, and substring is a subset of this sequence. Slicing in Python will help you to obtain substring from a given string. You can do many operations on this substring with the help of in-built functions present in the Python standard library.
So this was all on substrings in Python. I hope you must be clear with substrings now and with all the methods you can use on substrings. Refer to the below link to learn Python with Coding Ninjas free trial
By Deeksha Sharma