How to Extract a Substring in Python
In this tutorial, you'll learn to extract a substring from a given string in Python.
What is a Substring in Python?
A substring is a part of a string that exists within another string.
Here is an example:
This tutorial teaches you how to extract a substring from the string.
The phrase teaches you how to extract is one of the substrings since it exists in the above string.
Several ways allow you to extract a substring from a given string. You can either use the string slicing, the slice() method, or write your custom program.
String Slicing
You can extract a substring from a given string by slicing the string.
To slice a string and extract a substring, you can use the following syntax:
string[start:end]
Remarks
- The string[start:end] returns a substring, starting at position start, and up to but not including position end.
- If start omitted, the slice starts from the beginning of the string.
- If end omitted, the slice starts at position 0 to the end of the string.
Let's take a look at an example:
article = "If you want to learn more about Python Programming, you can check out the following links!"
substring = article[:20]
print (substring)
Output:
If you want to learn
Here, as you can see, the start position is omitted, so the slice starts at position 0 to the 20th position of the string.
Here is another example:
article = "If you want to learn more about Python Programming, you can check out the following links!"
substring = article[20:]
print (substring)
Output:
more about Python Programming, you can check out the following links!
Here, since the end position omitted, the slice starts at the position 20th to the end of the string.
Python slice() Method
The syntax of the slice() method is as follows:
slice(start, stop [, step])
The following is an example of using the slice() method to extract a substring, starting position 0 to 20:
article = "Python Programming, C++ Programming, Java Programming, PHP Programming, C# Programming, Ruby Programming";
slice_object = slice(20)
substring = article[slice_object]
print (substring)
Output:
Python Programming,
The following illustrates an example of how to extract a substring starting at position 10 to 30:
article = "Python Programming, C++ Programming, Java Programming, PHP Programming, C# Programming, Ruby Programming";
slice_object = slice(10, 30)
substring = article[slice_object]
print (substring)
Result:
gramming, C++ Progra
The following code extracts a substring starting at position 20 to the end of the string:
article = "C++ Programming, PHP Programming, Python Programming, Python Programming, Java Programming, C# Programming, C Programming"
length = len(article)
slice_object = slice(20, length)
substring = article[slice_object]
print (substring)
Output:
Programming, Python Programming, Python Programming, Java Programming, C# Programming, C Programming
Writing a Simple Program to Extract a Substring
You can also write a custom program to get the same result as the slice() method or string slicing.
Here is an example:
article = "Python Programming, C++ Programming, Java Programming, PHP Programming, C# Programming, Ruby Programming"index = 0length = len(article)start = 10end = lengthsubstring = ""while index < length:if index >= start and index < end:substring += article[index]index += 1print(substring)
This program extracts a substring starting at position 10 to the end of the string.
Output:
gramming, C++ Programming, Java Programming, PHP Programming, C# Programming, Ruby Programming
In this tutorial, you've learned how to extract a substring from a given string in Python. Python provides a useful built-in method, slice(), that allows you to extract a substring from the string. However, you can also write a custom program to get the same result.