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
+2-31
Original file line number
Diff line number
Diff line change
@@ -6,14 +6,14 @@ Subjects:
6
6
- 'Computer Science'
7
7
- 'Data Science'
8
8
Tags:
9
-
- 'Methods'
10
9
- 'Strings'
10
+
- 'Methods'
11
11
CatalogContent:
12
12
- 'learn-python-3'
13
13
- 'paths/computer-science'
14
14
---
15
15
16
-
A **substring** is a sequence of characters that are part of an original [string](https://www.codecademy.com/resources/docs/python/strings). In Python, substrings can be obtained by using the slicing feature on a string[variable](https://www.codecademy.com/resources/docs/python/variables). A slice can be made in a specific position within the string or it can be made at the default index.
16
+
A substring is a sequence of characters that are part of an original [string](https://www.codecademy.com/resources/docs/python/strings). In Python, substrings can be obtained by using the slicing feature on a string. A slice can be made in a specific position within the string or it can be made at the default index.
17
17
18
18
## Syntax
19
19
@@ -102,32 +102,3 @@ The string method [`.find()`](https://www.codecademy.com/resources/docs/python/s
102
102
name = "Code Ninja"
103
103
print(name.find('ni'))
104
104
```
105
-
106
-
## Best practices for using substrings in Python
107
-
108
-
-**Use slicing for efficiency:** String slicing is optimized in Python and should be preferred over loops for extracting substrings.
109
-
-**Utilize `in` for membership checks:** The `in` operator is the most efficient way to check if a substring exists within a string.
110
-
-**Normalize case before comparisons:** Convert strings to lowercase or uppercase to avoid case-sensitive mismatches.
111
-
-**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.
112
-
113
-
## FAQs
114
-
115
-
<details>
116
-
<summary>1. What happens if the end index is beyond the string length in slicing?</summary>
117
-
<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>
118
-
</details>
119
-
120
-
<details>
121
-
<summary>2. How can I check if a substring exists within a string?</summary>
122
-
<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>
123
-
</details>
124
-
125
-
<details>
126
-
<summary>3. Why should I use `.find()` instead of `.index()`?</summary>
127
-
<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>
128
-
</details>
129
-
130
-
<details>
131
-
<summary>4. Can I use negative indexes for substring extraction?</summary>
132
-
<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>
0 commit comments