Substring in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

A substring in Python is considered as a continuous portion of a string. The complete string is also considered as a substring. We can generate a substring in Python in several ways which we obviously discussed in the article. But one of the most widely used method for the generation of substring in Python is with the help of the slice operation.

What is Substring in Python?

Ever wondered, how would a car company find out the last 5 digits of CHASSIS NUMBER in such a fast and efficient manner? Answer to this thought lies under the concept of SUBSTRINGS. Read along to know more about substrings.

A substring is a continuous segment of character within a string. In simpler words, Substring is a continuous portion of a string. The Python string specifies several techniques for constructing a substring, checking if it includes a substring, and much more. Substring in python example

For example,

cad is a substring of string Scaler Academy.

Read along to know more about how we create substring, what are different methods for the creation of substring, and much more.

How to Generate a Substring from a Given String?

We have plenty of methods for creation of substring, which we will study in detail when I move along with the article. The best creation of substring is by the use slicing operator.

Let's understand the syntax of Slicing operator in detail.

Syntax of Slicing Operator

As we read earlier slicing operator is one of the best way that is used for creation of substring. Let's understand its syntax:

where,

startIndex: The starting index of the substring. The character at this index is included in the substring. If we don't set the startIndex value then, it is assumed to equal to 0.

endIndex: The end/last index of the substring. The character at this index is not included in the substring. If we don't set ther endIndex then, it is assumed to be equal to the length of the string by default.

step: Every step character after the current character which is to be included in substring. The default value is 1.

Different Methods of Slicing Strings in Python

There are several ways for creation of substring, but most of the methods are of slicing operator we can use it in different forms to get different kind of output. Let's understand one by one with help of example in detail.

Using Start Index and End Index ([start])

When we specify the start index and the end index, in the slicing operator then this will generate a substring which includes the starting index but excludes the ending index. Let's check the example of this case.

Example

In this example we are slicing the original string by passing both start and end value.

Using start index and end index

Output

Explanation

  • Firstly, we have created an original string.
  • After that, we used slicing operator in which we pass startIndex and the endIndex.
  • Finally, we got the output where the character at startIndex is included while character at endIndex is excluded.

Using Start Index without End Index ([start:])

When we specify the start index but don't specify the end index, in the slicing operator then this will generate a substring which includes the starting index and create substring till the end of the string. Let's check the example of this case.

Example

In this example we are slicing the original string by only passing start value.

Using start index without end index

Output

Explanation

  • Firstly, we have created an original string.
  • After that, we used slicing operator in which we pass startIndex.
  • Finally, we got the output where the character at startIndex is included and the substring is generated till the end of the string.

Using End Index without Start Index ([])

When we specify the end index but don't specify the start index, in the slicing operator then this will generate a substring from the start of the string and end the substring where the end index is specified(which is excluded from the substring). Let's check the example of this case.

Example

In this example we are slicing the original string by only passing end value.

Using end index without start index

Output

Explanation

  • Firstly, we have created an original string.
  • After that, we used slicing operator in which we pass the endIndex.
  • Finally, we got the output where the substring starts from the beginning of the string and ends at the position where endIndex is specified.

Using Complete String ([:])

When we don't specify the start index and the end index, in the slicing operator then this will generate a substring beginning to the end of the string. In other words, it will slice the complete string. Let's check the example of this case.

Example

In this example, we are slicing the original string by passing no value in the slicing operator.

Using complete string

Output

Explanation

  • Firstly, we have created an original string.
  • After that, we used slicing operator in which don't pass any of the parameter.
  • Finally, we got the output where the whole string is sliced.

Using Single Character from a String ([index])

When we just specify the single index , in the slicing operator then this will return a single character which is present at that particular index. Let's check the example of this case.

Example

In this example we are slicing the original string by only passing a single index position.

Using single character from a string

Output

Explanation

  • Firstly, we have created an original string.
  • After that, we used slicing operator in which we pass a single index.
  • Finally, we got the output where the character at the specified index is printed.

Using Start Index, End Index, and Step (start : end : step)

When we specify the start index, end index, and the steps then this will generate a substring from start index (inclusive) to the end index (exclusive) where every character is at an interval of steps which are passed in the parameter. By default value of steps is 1.

Example

In this example, we are slicing the original string by passing start, end, and step values.

 slicing the original string by passing start end and the steps value

Output

Explanation

  • Firstly, we have created an original string.
  • After that, we used slicing operator in which we pass startIndex and the endIndex and the step.
  • Finally, we got the output where the character at startIndex is included while character at endIndex is excluded and every character is at an interval of steps which are passed in the parameter.

Using Negative Index ([-index])

As we know, Python also support -ve indexing. The letters of the string when traversed from right to left are indexed with negative numbers.

Using negative index

This image will help you to clarify the -ve indexing easily.

Example

In this example we are slicing the original string by passing negative(-) values.

Output

Explanation

  • First of all, we have store the string whose substring is to be created into a variable.
  • Then we used slicing operator and passed -ve index to it.
  • At last, we print the output to get the substring.

Using Positive Index ([index])

Irrespective of previous case, we are using positive index for generation of substring.

Example

In this example we are slicing the original string by only passing positive(+) values.

Using positive index

Output

Explanation

  • First of all, we have store the string whose substring is to be created into a variable.
  • Then we used slicing operator and passed +ve index to it.
  • At last, we print the output to get the substring.

Using List Comprehension

List comprehensions are used for creating new lists from other iterables like tuples, strings, arrays, lists, etc. A list comprehension consists of brackets containing the expression, which is executed for each element along with the for loop to iterate over each element.

List comprehension is a way that helps us to create a new list based on the values of an existing list in a shorter manner.

Syntax

This returns the new list, leaving the old list unchanged.

Using list comprehension

The combination of list comprehension and string slicing can be used to get all the substring that can be generated by a string.

Example

We are going to create all the possible substring that can be generated by word SCALER.

Output

Explanation

  • First of all we have created a string which stores the value of strings whose substrings have to be generated.
  • Then we use List comprehension technique, inside which we have used slicied operator and the starting and ending position is judged by the outer(loop for iteration of i) and inner loops(loop for iteration of j) respectively.
  • Then, we print array of all substring.

Using itertools.combination()

The process of generation of all substrings of the string can also be performed using the inbuilt function of combinations of itertools library, which helps to get all the possible combinations i.e. the substrings from a string.

Example

We are going to generate all the substrings of string using inbuilt library function combination.

Output

Explanation

  • We have imported the inbuilt function combinations from the itertools library.
  • THen, we create a string whose substrings are to be generated and stored it into a variable.
  • Then we used itertools combination function for creation of start index and end index for generation of substring
  • At the end, we print the array of all the substring and get the desired output.

Click Here, To know more about itertools in python.

Check If Python String Contains Substring

Using in Operator

The easiest way to check if a Python string contains a substring is to use the in operator. It returns a Boolean (either True or False).

Example

Output

Explanation We created an original string and a sliced string(substring) and stored them into 2 different variables. Then, we use if-else conditioning statements into which we use in statement whether the substring is present in string or not. At last we got desired output whether the substring is present in string or not.

Using String.index() Method

The String type in Python has a method called index() that can be used to find the starting index of the first occurrence of a substring in a string. If the substring is not found in the string then it will raise the error which we have to handle with the help of try-exception statement.

Syntax

Index function which is used on string is used to find the index of the character present in the string. It takes 3 parameter:

  • Value: Value whose index is to be found in the string.
  • Start: Starting index. Default Value is 0.
  • End: Ending index. Default Value is end of the string.

Example

Output

Explanation

  • We created an original string and a sliced string(substring) and stored them into 2 different variables.
  • Then, we use try-exception-else conditioning statements into which we use index() to check the first occurrence index of the substring.
  • At last we got desired output whether the substring is present in string or not. If the substring is not present then we handled the error with the help of try-exception block.

Using String.find() Method

The String type has another method called find which is more convenient to use than index(), because we don't need to worry about handling any exceptions. It just returns the index of the first occurrence of substring which is found in the string.

If find() doesn't find a match, it returns -1, otherwise it returns the left-most index of the substring in the larger string.

Syntax

Find function which is used on string is used to find the index of the character present in the string. It takes parameter:

  • Value: Value whose index is to be find the string.
  • Start: Starting index. Default Value is 0.
  • End: Ending index. Default Value is end of the string.

Example

Output

Explanation

We created an original string and a sliced string(substring) and stored them into 2 different variables. Then, we use if-else conditioning statements into which we use find() statement whether the substring is present in string or not. At last we got desired output whether the substring is present in string or not. If the substring is not present in the string then find function will return -1.

Using Regular Expression

Regular expressions provide a more flexible way to check strings for pattern matching. re module is used for using regular expressions in python. The re module contains a function called search(), which we can use to match a substring pattern.

Example

Output

Explanation We created an original string and a sliced string(substring) and stored them into 2 different variables. Then, we use if-else conditioning statements into which we use search statement whether the substring is present in string or not. At last we got desired output whether the substring is present in string or not.

Count of Substring Occurrence

We can use count() function to find the number of occurrences of a word or a substring in the string.

As we are aware with the count function in python. Let's see the example how it is used to find the occurrence of a substring in a string.

Example

Output

Explanation

  • We created an original string and stored it into a variable.
  • After that, we created 2 different substrings and stored them into different variables.
  • Finally we have used the count() to find the frequency of each susbtring into the string one by one and print them onto the output screen.

Find All Index of Substring

There is no built-in function to get the list of all the indexes for the substring. We need to create a user defined function which is used for finding all the index of substring using find() function.

Example

Output

Explanation

  • We have created a user-defined function that accepts 2 parameters original string and the substring.
  • Then we start the loop till we iterate the complete string.
    • Inside it, we have used find() which returns the first occurence index of the susbtring from the main string
    • If the susbtring is not present then it will return -1
  • After creation of user defined function, we called that function and got the desired output.

Predefined String Methods

We are going to discuss some methods that are used to manipulate the strings in Python. These are:

S.No.MethodDescription
1Center()The center() method will center align the string, using a specified character (space is default) as the fill character.
2Count()The count() method returns the number of elements with the specified value.
3Capitalize()In Python, the capitalize() method converts first character of a string to uppercase letter and lowercases all other characters, if any.
4decode()This method is used to convert from one encoding scheme, in which argument string is encoded to the desired encoding scheme.
5encode()The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.
6endswith()The endswith() method returns True if the string ends with the specified value, otherwise False.
7expandtabs()The expandtabs() method sets the tab size to the specified number of whitespaces.
8find()The find() method finds the first occurrence of the specified value.
9index()The index() method returns the position at the first occurrence of the specified value.
10isalpha()The isalpha() method returns True if all the characters are alphabet letters (a-z).
11isalnum()The isalnum() method returns True if all the characters are alphanumeric, meaning alphabet letter (a-z) and numbers (0-9).
12islower()The islower() method returns True if all the characters are in lower case, otherwise False.
13isdigit()The isdigit() method returns True if all the characters are digits, otherwise False.
14istitle()The istitle() method returns True if all words in a text start with a upper case letter, AND the rest of the word are lower case letters, otherwise False.
15isnumeric()The isnumeric() method returns True if all the characters are numeric (0-9), otherwise False.
16isspace()The isspace() method returns True if all the characters in a string are whitespaces, otherwise False.
17isupper()The isupper() method returns True if all the characters are in upper case, otherwise False.
18strip()The strip() method returns a copy of the string by removing both the leading and the trailing characters (based on the string argument passed).
19isdecimal()The isdecimal() method returns True if all the characters are decimals (0-9).
20join()The join() method takes all items in an iterable and joins them into one string.
21len()The len() function returns the number of items in an object.
22lower()The lower() method returns a string where all characters are lower case.
23ljust()The ljust() method will left align the string, using a specified character (space is default) as the fill character.
24maketrans()The maketrans() method returns a mapping table that can be used with the translate() method to replace specified characters.
25max()The max() function returns the item with the highest value, or the item with the highest value in an iterable.
26min()The min() function returns the item with the lowest value, or the item with the lowest value in an iterable.
27rjust()The rjust() method will right align the string, using a specified character (space is default) as the fill character.
28replace()The replace() method replaces a specified phrase with another specified phrase.
29rfind()The rfind() method in python finds the last occurrence of the specified value.
30rindex()The rindex() method finds the last occurrence of the specified value.
31rstrip()The rstrip() method removes any trailing characters (characters at the end a string), space is the default trailing character to remove.
32strip()The strip() method in python removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to remove)
33startswith()The startswith() method returns True if the string starts with the specified value, otherwise False.
34split()The split() method splits a string into a list.
35spilitlines()The splitlines() method in python splits a string into a list. The splitting is done at line breaks.
36swapcase()The swapcase() method returns a string where all the upper case letters are lower case and vice versa.
37translate()The translate() method returns a string where some specified characters are replaced with the character described in a dictionary, or in a mapping table.
38title()The title() method returns a string where the first character in every word is upper case. Like a header, or a title.
39upper()The upper() method returns a string where all characters are in upper case.
40zfill()The zfill() method adds zeros (0) at the beginning of the string, until it reaches the specified length.

Learn More

Check out this article and learn about which module in Python supports regular expressions by clicking here.

Explore Scaler Topics Python Tutorial and enhance your Python skills with Reading Tracks and Challenges.

Conclusion

  • First of all, we understand what is substring in Python.
  • Then, we understand we can create substring in python.
  • Then, we go through several methods for the creation of substring in Python.
  • After that, we studied various methods that help us to check whether a substring is present in a string or not.
  • Applications of substrings are to find last 4 digits of a mobile number, or last 5 digits of chassis number, or to check whether a character is present in a word or not.
  • Last but not least, we understand 40 different methods which we can apply on string to get different kinds of results.