Skip to content

Commit f980cac

Browse files
[Edit] Python: substrings
1 parent 32233ac commit f980cac

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

content/python/concepts/substrings/substrings.md

+31-2
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ Subjects:
66
- 'Computer Science'
77
- 'Data Science'
88
Tags:
9-
- 'Strings'
109
- 'Methods'
10+
- 'Strings'
1111
CatalogContent:
1212
- 'learn-python-3'
1313
- 'paths/computer-science'
1414
---
1515

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. 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 [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.
1717

1818
## Syntax
1919

@@ -102,3 +102,32 @@ The string method [`.find()`](https://www.codecademy.com/resources/docs/python/s
102102
name = "Code Ninja"
103103
print(name.find('ni'))
104104
```
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>
133+
</details>

0 commit comments

Comments
 (0)