Skip to content

Commit 3ec88f7

Browse files
authored
[Edit] Python Strings: .endswith() (#6611)
1 parent 72ac633 commit 3ec88f7

File tree

1 file changed

+56
-24
lines changed
  • content/python/concepts/strings/terms/endswith

1 file changed

+56
-24
lines changed

content/python/concepts/strings/terms/endswith/endswith.md

+56-24
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,90 @@ Tags:
88
- 'Functions'
99
- 'Methods'
1010
- 'Strings'
11+
- 'Values'
1112
CatalogContent:
1213
- 'learn-python-3'
1314
- 'paths/computer-science'
14-
- 'paths/data-science'
1515
---
1616

17-
The `.endswith()` method checks a value against a given string and returns `True` if the string ends with that value. Otherwise, it returns `False`.
17+
The **`.endswith()`** method checks a value against a given string and returns `True` if the string ends with that value. Otherwise, it returns `False`. This method is particularly useful when filtering or validating strings, such as verifying file extensions or checking URL endings.
1818

1919
## Syntax
2020

2121
```pseudo
2222
string.endswith(value, start, end)
2323
```
2424

25-
Given a string, the `.endswith()` method can be used in the following way:
25+
**Parameters:**
2626

27-
- The required `value` argument, which is checked if it exists at the end of the string. It is also case-sensitive.
27+
- `value`: The string or [tuple](https://www.codecademy.com/resources/docs/python/tuples) of strings to check for.
28+
- `start` (Optional): The position in the original string where the search should start.
29+
- `end` (Optional): The position in the original string where the search should end.
2830

29-
- Optionally, the `value` can be tested for whether it is at the end of a particular substring within the string using the `start` and `end` index arguments.
31+
**Return value:**
3032

31-
## Example
33+
The method returns `True` if the original string ends with the given value and `False` otherwise.
34+
35+
## Example 1: `.endswith()` Without `start` and `end` Parameters
36+
37+
This example uses the `.endswith()` method without `start` and `end` parameters to verify if a string ends with the given value:
3238

3339
```py
3440
example_str = "This is a string"
3541

36-
check_A = example_str.endswith("g")
37-
check_B = example_str.endswith("s")
38-
check_C = example_str.endswith("st", 5, 12)
42+
check = example_str.endswith("g")
43+
44+
print(check)
45+
```
46+
47+
Since the string ends with the given value, the example produces this output:
48+
49+
```shell
50+
True
51+
```
52+
53+
## Example 2: `.endswith()` with `start` and `end` Parameters
54+
55+
This example uses the `.endswith()` method with `start` and `end` parameters to check if the substring from index 8-13 in a string ends with the given value:
56+
57+
```py
58+
text = "holiday_photo.jpg"
59+
60+
res = text.endswith("photo", 8, 13)
3961

40-
print("A: ", check_A)
41-
print("B: ", check_B)
42-
print("C: ", check_C)
62+
print(res)
4363
```
4464

45-
The output will look like this:
65+
Since the substring from index 8-13 in the string ends with the given value, the example produces this output:
4666

4767
```shell
48-
A: True
49-
B: False
50-
C: True
68+
True
5169
```
5270

53-
From the above example, for `check_A` the output is `True` as the `.endswith()` function checks whether the `example_str` string ends with the character "g".
71+
## Codebyte Example: `.endswith()` with a Tuple of Strings
5472

55-
## Codebyte Example
73+
This codebyte example uses the `.endswith()` method to check if a string ends with any of the values provided in the given tuple:
5674

5775
```codebyte/python
58-
example_A = "This is a string"
59-
check_A = example_A.endswith("ring")
60-
print(check_A)
76+
filename = "document.txt"
77+
78+
res = filename.endswith((".txt", ".docx", ".pdf"))
6179
62-
example_B = "This is a string"
63-
check_B = example_B.endswith("G")
64-
print(check_B)
80+
print(res)
6581
```
82+
83+
Since the string ends with `".txt"`, the output for the code will be `True`.
84+
85+
## Frequently Asked Questions
86+
87+
### 1. How does `.endswith()` and `.startswith()` differ?
88+
89+
The `.endswith()` method checks if a string ends with a specific suffix, while [`.startswith()`](https://www.codecademy.com/resources/docs/python/strings/startswith) checks if it begins with a specific prefix.
90+
91+
### 2. Is Python `.endswith()` case-sensitive?
92+
93+
Yes, `.endswith()` is case-sensitive.
94+
95+
### 3. Can `.endswith()` be used with empty strings?
96+
97+
Yes. Any string ends with an empty string, so calling `.endswith("")` on any string will return `True`.

0 commit comments

Comments
 (0)