You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/python/concepts/substrings/substrings.md
+18-22Lines changed: 18 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
Title: 'Substrings'
3
-
Description: 'A substring is a sequence of characters that are part of an original string.'
3
+
Description: 'A substring in Python is a contiguous sequence of characters extracted from an original string.'
4
4
Subjects:
5
5
- 'Code Foundations'
6
6
- 'Computer Science'
@@ -25,9 +25,9 @@ A slice is made by using the open `[` and closed `]` square brackets next to a s
25
25
string[start:end:step]
26
26
```
27
27
28
-
-`start` defaults to 0 and gives the initial position the slice will start from.
29
-
-`end` defaults to -1 and is the position where the slicing will end.
30
-
-`step` defaults to 1 and indicates the number of steps to take in between indexes.
28
+
-`start`: Defaults to `0` and gives the initial position the slice will start from.
29
+
-`end`: Defaults to the length of the string and marks the position where the slice stops (not inclusive).
30
+
-`step`: Defaults to `1` and indicates the number of steps to take between indexes.
31
31
32
32
## Examples
33
33
@@ -57,7 +57,7 @@ print(name[-1])
57
57
58
58
### Negative Start Index
59
59
60
-
Using a negative start index (`-n`) with the default end value accesses the last `n` characters of the string. The following gives access to the last three characters of the string:
60
+
Using a negative start index (`-n`) with the default end value accesses the last `n` characters of the string. The following returns the last three characters of the string:
61
61
62
62
```py
63
63
print(name[-3:])
@@ -112,24 +112,20 @@ print(name.find('ni'))
112
112
-**Normalize case before comparisons:** Convert strings to lowercase or uppercase to avoid case-sensitive mismatches.
113
113
-**Handle missing substrings gracefully:** Use `.find()` instead of [`.index()`](https://www.codecademy.com/resources/docs/python/strings/index) when searching for substrings to avoid exceptions.
114
114
115
-
## FAQs
115
+
## Frequently Asked Questions
116
116
117
-
<details>
118
-
<summary>1. What happens if the end index is beyond the string length in slicing?</summary>
119
-
<p>If the end index exceeds the string length, Python does not raise an error. It simply returns the available portion of the string.</p>
120
-
</details>
117
+
### 1. What happens if the end index is beyond the string length in slicing?
121
118
122
-
<details>
123
-
<summary>2. How can I check if a substring exists within a string?</summary>
124
-
<p>You can use the `in` operator or the `.find()` method. The `in` operator returns `True` or `False`, while `.find()` returns the starting index of the substring or `-1` if not found.</p>
125
-
</details>
119
+
If the end index exceeds the string length, Python does not raise an error. It simply returns the available portion of the string.
126
120
127
-
<details>
128
-
<summary>3. Why should I use `.find()` instead of `.index()`?</summary>
129
-
<p>The `.find()` method returns `-1` if the substring is not found, whereas `.index()` raises an exception. If you don’t want to handle exceptions manually, `.find()` is a safer choice.</p>
130
-
</details>
121
+
### 2. How can I check if a substring exists within a string?
131
122
132
-
<details>
133
-
<summary>4. Can I use negative indexes for substring extraction?</summary>
134
-
<p>Yes, Python allows negative indexing, which counts from the end of the string. You can use negative values for both the start and end positions when slicing.</p>
135
-
</details>
123
+
You can use the `in` operator or the `.find()` method. The `in` operator returns `True` or `False`, while `.find()` returns the starting index of the substring or `-1` if not found.
124
+
125
+
### 3. Why should I use `.find()` instead of `.index()`?
126
+
127
+
The `.find()` method returns `-1` if the substring is not found, whereas `.index()` raises an exception. If you don’t want to handle exceptions manually, `.find()` is a safer choice.
128
+
129
+
### 4. Can I use negative indexes for substring extraction?
130
+
131
+
Yes, Python allows negative indexing, which counts from the end of the string. You can use negative values for both the start and end positions when slicing.
0 commit comments