Skip to content

Commit 796f67a

Browse files
[Edit] Python: substrings (#7065)
* [Edit] SQL: DATEDIFF() * Update datediff.md * [Edit] Python: substrings * Update content/python/concepts/substrings/substrings.md * Update content/python/concepts/substrings/substrings.md ---------
1 parent 2479314 commit 796f67a

File tree

1 file changed

+18
-22
lines changed

1 file changed

+18
-22
lines changed

content/python/concepts/substrings/substrings.md

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
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.'
44
Subjects:
55
- 'Code Foundations'
66
- 'Computer Science'
@@ -25,9 +25,9 @@ A slice is made by using the open `[` and closed `]` square brackets next to a s
2525
string[start:end:step]
2626
```
2727

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.
3131

3232
## Examples
3333

@@ -57,7 +57,7 @@ print(name[-1])
5757

5858
### Negative Start Index
5959

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:
6161

6262
```py
6363
print(name[-3:])
@@ -112,24 +112,20 @@ print(name.find('ni'))
112112
- **Normalize case before comparisons:** Convert strings to lowercase or uppercase to avoid case-sensitive mismatches.
113113
- **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.
114114

115-
## FAQs
115+
## Frequently Asked Questions
116116

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?
121118

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.
126120

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?
131122

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

Comments
 (0)