1 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>When we work with strings in programming, we regularly have to extract parts of them.</p>
1
<p>When we work with strings in programming, we regularly have to extract parts of them.</p>
2
<p>For example, we need to find a smaller string inside a larger one. In this lesson, we'll look at how to do it.</p>
2
<p>For example, we need to find a smaller string inside a larger one. In this lesson, we'll look at how to do it.</p>
3
<h2>Substrings and slices for strings</h2>
3
<h2>Substrings and slices for strings</h2>
4
<p><strong>A substring</strong>is part of a string that needs to be found and extracted.</p>
4
<p><strong>A substring</strong>is part of a string that needs to be found and extracted.</p>
5
<p>Suppose we have a date in this format:<em>12-08-2034</em>. We need to extract a substring from it that only has the year.</p>
5
<p>Suppose we have a date in this format:<em>12-08-2034</em>. We need to extract a substring from it that only has the year.</p>
6
<p>If you think about it logically, you have to calculate the index from the first character of the year and extract the four characters. The indices in the string start with zero, so the first character of the year is available at index 6, and the last character is at index 9. Let's check it out:</p>
6
<p>If you think about it logically, you have to calculate the index from the first character of the year and extract the four characters. The indices in the string start with zero, so the first character of the year is available at index 6, and the last character is at index 9. Let's check it out:</p>
7
<p>Now we know these indices, we can use slices and get the desired substring:</p>
7
<p>Now we know these indices, we can use slices and get the desired substring:</p>
8
<p>In Python,<strong>string slices</strong>are a mechanism by which we extract a substring according to specified parameters. In the example above, we took a substring from index 6 up to but not including index 10, meaning from 6 to 9 inclusive. The formula looks like this:</p>
8
<p>In Python,<strong>string slices</strong>are a mechanism by which we extract a substring according to specified parameters. In the example above, we took a substring from index 6 up to but not including index 10, meaning from 6 to 9 inclusive. The formula looks like this:</p>
9
<p>Slices are a tool with many variations. For example, if we don't specify a second boundary, we extract all the characters up to the end of the string. It applies to the first boundary, meaning the beginning of the line:</p>
9
<p>Slices are a tool with many variations. For example, if we don't specify a second boundary, we extract all the characters up to the end of the string. It applies to the first boundary, meaning the beginning of the line:</p>
10
<p>You can even specify negative indices. In this case, it starts from the opposite side:</p>
10
<p>You can even specify negative indices. In this case, it starts from the opposite side:</p>
11
<p>Slices have two mandatory parameters, but sometimes we can use a third one.</p>
11
<p>Slices have two mandatory parameters, but sometimes we can use a third one.</p>
12
+
<h2>Extraction steps</h2>
12
<p>Slices have a third optional parameter,<strong>extraction step</strong>. It's 1 by default, but we can change it:</p>
13
<p>Slices have a third optional parameter,<strong>extraction step</strong>. It's 1 by default, but we can change it:</p>
13
<p>All of these can be combined with open boundaries, in other words, without setting a beginning or end:</p>
14
<p>All of these can be combined with open boundaries, in other words, without setting a beginning or end:</p>
14
<p>The step can be negative. In this case, we work with it from the end. From this comes the most popular way to use the extraction step; to<strong>reverse the string</strong>:</p>
15
<p>The step can be negative. In this case, we work with it from the end. From this comes the most popular way to use the extraction step; to<strong>reverse the string</strong>:</p>
15
<p>If we use a negative step and extract the slice elements in reverse order, we should also specify the slice boundaries in reverse order. We indicate at first the right slice boundary and then the left one:</p>
16
<p>If we use a negative step and extract the slice elements in reverse order, we should also specify the slice boundaries in reverse order. We indicate at first the right slice boundary and then the left one:</p>
16
<p>The slices can be specified using variables as well as numbers:</p>
17
<p>The slices can be specified using variables as well as numbers:</p>
17
<p>As you can see, slices do a lot. Don't worry if you don't remember all these combinations right now. Later, you'll learn how to use them without looking at the documentation.</p>
18
<p>As you can see, slices do a lot. Don't worry if you don't remember all these combinations right now. Later, you'll learn how to use them without looking at the documentation.</p>