|
8 | 8 | - 'Functions'
|
9 | 9 | - 'Methods'
|
10 | 10 | - 'Strings'
|
| 11 | + - 'Values' |
11 | 12 | CatalogContent:
|
12 | 13 | - 'learn-python-3'
|
13 | 14 | - 'paths/computer-science'
|
14 |
| - - 'paths/data-science' |
15 | 15 | ---
|
16 | 16 |
|
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. |
18 | 18 |
|
19 | 19 | ## Syntax
|
20 | 20 |
|
21 | 21 | ```pseudo
|
22 | 22 | string.endswith(value, start, end)
|
23 | 23 | ```
|
24 | 24 |
|
25 |
| -Given a string, the `.endswith()` method can be used in the following way: |
| 25 | +**Parameters:** |
26 | 26 |
|
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. |
28 | 30 |
|
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:** |
30 | 32 |
|
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: |
32 | 38 |
|
33 | 39 | ```py
|
34 | 40 | example_str = "This is a string"
|
35 | 41 |
|
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) |
39 | 61 |
|
40 |
| -print("A: ", check_A) |
41 |
| -print("B: ", check_B) |
42 |
| -print("C: ", check_C) |
| 62 | +print(res) |
43 | 63 | ```
|
44 | 64 |
|
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: |
46 | 66 |
|
47 | 67 | ```shell
|
48 |
| -A: True |
49 |
| -B: False |
50 |
| -C: True |
| 68 | +True |
51 | 69 | ```
|
52 | 70 |
|
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 |
54 | 72 |
|
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: |
56 | 74 |
|
57 | 75 | ```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")) |
61 | 79 |
|
62 |
| -example_B = "This is a string" |
63 |
| -check_B = example_B.endswith("G") |
64 |
| -print(check_B) |
| 80 | +print(res) |
65 | 81 | ```
|
| 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