TutorialAce

Python | Java | MySQL | Excel

Python Tutorial

Python Substring with Examples

How to Extract a Substring in Python

In this tutorial, we'll learn to extract a substring from a given string in Python.

What is a Substring in Python?

A substring is a string that exists within another string. For example, let's take a look at the following string:

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.

There are several ways that allow you to extract a substring from a given string. You can either use string slicing, the slice() method or write you own program.

String Slicing

You can extract a substring from a string by slicing the string.

To extract a substring from a string, you can use the following syntax:

string[start:end]

Note that string[start:end] returns a substring, starting at position start, and up to but not including position end. If you omit the start position, the slice starts the beginning of the string. And if you omit the end position, 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)

Result:

If you want to learn

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)

more about Python Programming, you can check out the following links!

Here, since the end position is omitted, the slice starts at the position 20th to the end of the string.

Using the 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)

Result:

Python Programming,

The following example illustrates how to extract a substring starting at position 10 to 30 using the slice() method:

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)

Result:

Programming, Python Programming, Python Programming, Java Programming, C# Programming, C Programming

Writing a Simple Program to Extract a Substring

You can also write own 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 = 0;
length = len(article)
start = 10
end = length
substring = ""
while index < length:
if index >= start and index < end:
substring += article[index]
index += 1
print(substring)

This program extracts a subtring starting at position 10 to the end of the string.

Result:

gramming, C++ Programming, Java Programming, PHP Programming, C# Programming, Ruby Programming

In this tutorial, we've learned how to extract a substring from a given string in Python. Python provides useful built-in method, slice(), that allows you to extract a substring from the string. However, you can also write your own program to get the same result.

Leave a Reply