|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<small><small><i>\n", |
| 8 | + "All the IPython Notebooks in this lecture series by Dr. Milan Parmar are available @ **[GitHub](https://github.com/milaan9/02_Python_Datatypes/tree/main/002_Python_String_Methods)**\n", |
| 9 | + "</i></small></small>" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "# Python String `endswith()`\n", |
| 17 | + "\n", |
| 18 | + "The **`endswith()`** method returns True if a string ends with the specified suffix. If not, it returns False.\n", |
| 19 | + "\n", |
| 20 | + "**Syntax**:\n", |
| 21 | + "\n", |
| 22 | + "```python\n", |
| 23 | + "str.endswith(suffix[, start[, end]])\n", |
| 24 | + "```" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "## `endswith()` Parameters\n", |
| 32 | + "\n", |
| 33 | + "The **`endswith()`** method takes three parameters:\n", |
| 34 | + "\n", |
| 35 | + "* **suffix** - String or tuple of suffixes to be checked.\n", |
| 36 | + "* **start (Optional)** - Beginning position where **suffix** is to be checked within the string.\n", |
| 37 | + "* **end (Optional)** - Ending position where **suffix** is to be checked within the string." |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "markdown", |
| 42 | + "metadata": {}, |
| 43 | + "source": [ |
| 44 | + "## Return Value from `endswith()`\n", |
| 45 | + "\n", |
| 46 | + "The **`endswith()`** method returns a boolean.\n", |
| 47 | + "\n", |
| 48 | + "* It returns **True** if strings ends with the specified suffix.\n", |
| 49 | + "* It returns **False** if string doesn't end with the specified suffix." |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "code", |
| 54 | + "execution_count": 1, |
| 55 | + "metadata": { |
| 56 | + "ExecuteTime": { |
| 57 | + "end_time": "2021-06-10T18:04:31.419064Z", |
| 58 | + "start_time": "2021-06-10T18:04:31.402512Z" |
| 59 | + } |
| 60 | + }, |
| 61 | + "outputs": [ |
| 62 | + { |
| 63 | + "name": "stdout", |
| 64 | + "output_type": "stream", |
| 65 | + "text": [ |
| 66 | + "False\n", |
| 67 | + "True\n", |
| 68 | + "True\n" |
| 69 | + ] |
| 70 | + } |
| 71 | + ], |
| 72 | + "source": [ |
| 73 | + "# Example 1: endswith() Without start and end Parameters\n", |
| 74 | + "\n", |
| 75 | + "text = \"Python is easy to learn.\"\n", |
| 76 | + "\n", |
| 77 | + "result = text.endswith('to learn')\n", |
| 78 | + "# returns False\n", |
| 79 | + "print(result)\n", |
| 80 | + "\n", |
| 81 | + "result = text.endswith('to learn.')\n", |
| 82 | + "# returns True\n", |
| 83 | + "print(result)\n", |
| 84 | + "\n", |
| 85 | + "result = text.endswith('Python is easy to learn.')\n", |
| 86 | + "# returns True\n", |
| 87 | + "print(result)" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": 2, |
| 93 | + "metadata": { |
| 94 | + "ExecuteTime": { |
| 95 | + "end_time": "2021-06-10T18:04:32.012611Z", |
| 96 | + "start_time": "2021-06-10T18:04:31.999952Z" |
| 97 | + } |
| 98 | + }, |
| 99 | + "outputs": [ |
| 100 | + { |
| 101 | + "name": "stdout", |
| 102 | + "output_type": "stream", |
| 103 | + "text": [ |
| 104 | + "True\n", |
| 105 | + "False\n", |
| 106 | + "True\n" |
| 107 | + ] |
| 108 | + } |
| 109 | + ], |
| 110 | + "source": [ |
| 111 | + "# Example 2: endswith() With start and end Parameters\n", |
| 112 | + "\n", |
| 113 | + "text = \"Python programming is easy to learn.\"\n", |
| 114 | + "\n", |
| 115 | + "# start parameter: 7\n", |
| 116 | + "# \"programming is easy to learn.\" string is searched\n", |
| 117 | + "result = text.endswith('learn.', 7)\n", |
| 118 | + "print(result)\n", |
| 119 | + "\n", |
| 120 | + "# Both start and end is provided\n", |
| 121 | + "# start: 7, end: 26\n", |
| 122 | + "# \"programming is easy\" string is searched\n", |
| 123 | + "\n", |
| 124 | + "result = text.endswith('is', 7, 26)\n", |
| 125 | + "# Returns False\n", |
| 126 | + "print(result)\n", |
| 127 | + "\n", |
| 128 | + "result = text.endswith('easy', 7, 26)\n", |
| 129 | + "# returns True\n", |
| 130 | + "print(result)" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "markdown", |
| 135 | + "metadata": {}, |
| 136 | + "source": [ |
| 137 | + "## Passing Tuple to `endswith()`\n", |
| 138 | + "\n", |
| 139 | + "It's possible to pass a tuple suffixes to the **`endswith()`** method in Python.\n", |
| 140 | + "\n", |
| 141 | + "If the string ends with any item of the tuple, **`endswith()`** returns **`True`**. If not, it returns **`False`**." |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": 3, |
| 147 | + "metadata": { |
| 148 | + "ExecuteTime": { |
| 149 | + "end_time": "2021-06-10T18:04:34.434512Z", |
| 150 | + "start_time": "2021-06-10T18:04:34.425746Z" |
| 151 | + } |
| 152 | + }, |
| 153 | + "outputs": [ |
| 154 | + { |
| 155 | + "name": "stdout", |
| 156 | + "output_type": "stream", |
| 157 | + "text": [ |
| 158 | + "False\n", |
| 159 | + "True\n", |
| 160 | + "True\n" |
| 161 | + ] |
| 162 | + } |
| 163 | + ], |
| 164 | + "source": [ |
| 165 | + "# Example 3: endswith() With Tuple Suffix\n", |
| 166 | + "\n", |
| 167 | + "text = \"programming is easy\"\n", |
| 168 | + "result = text.endswith(('programming', 'python'))\n", |
| 169 | + "\n", |
| 170 | + "# prints False\n", |
| 171 | + "print(result)\n", |
| 172 | + "\n", |
| 173 | + "result = text.endswith(('python', 'easy', 'java'))\n", |
| 174 | + "\n", |
| 175 | + "#prints True\n", |
| 176 | + "print(result)\n", |
| 177 | + "\n", |
| 178 | + "# With start and end parameter\n", |
| 179 | + "# 'programming is' string is checked\n", |
| 180 | + "result = text.endswith(('is', 'an'), 0, 14)\n", |
| 181 | + "\n", |
| 182 | + "# prints True\n", |
| 183 | + "print(result)" |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "markdown", |
| 188 | + "metadata": {}, |
| 189 | + "source": [ |
| 190 | + "If you need to check if a string starts with the specified prefix, you can use **[startswith() method in Python](https://github.com/milaan9/02_Python_Datatypes/blob/main/002_Python_String_Methods/041_Python_String_startswith%28%29.ipynb)**." |
| 191 | + ] |
| 192 | + }, |
| 193 | + { |
| 194 | + "cell_type": "code", |
| 195 | + "execution_count": null, |
| 196 | + "metadata": {}, |
| 197 | + "outputs": [], |
| 198 | + "source": [] |
| 199 | + } |
| 200 | + ], |
| 201 | + "metadata": { |
| 202 | + "hide_input": false, |
| 203 | + "kernelspec": { |
| 204 | + "display_name": "Python 3", |
| 205 | + "language": "python", |
| 206 | + "name": "python3" |
| 207 | + }, |
| 208 | + "language_info": { |
| 209 | + "codemirror_mode": { |
| 210 | + "name": "ipython", |
| 211 | + "version": 3 |
| 212 | + }, |
| 213 | + "file_extension": ".py", |
| 214 | + "mimetype": "text/x-python", |
| 215 | + "name": "python", |
| 216 | + "nbconvert_exporter": "python", |
| 217 | + "pygments_lexer": "ipython3", |
| 218 | + "version": "3.8.8" |
| 219 | + }, |
| 220 | + "toc": { |
| 221 | + "base_numbering": 1, |
| 222 | + "nav_menu": {}, |
| 223 | + "number_sections": true, |
| 224 | + "sideBar": true, |
| 225 | + "skip_h1_title": false, |
| 226 | + "title_cell": "Table of Contents", |
| 227 | + "title_sidebar": "Contents", |
| 228 | + "toc_cell": false, |
| 229 | + "toc_position": {}, |
| 230 | + "toc_section_display": true, |
| 231 | + "toc_window_display": false |
| 232 | + }, |
| 233 | + "varInspector": { |
| 234 | + "cols": { |
| 235 | + "lenName": 16, |
| 236 | + "lenType": 16, |
| 237 | + "lenVar": 40 |
| 238 | + }, |
| 239 | + "kernels_config": { |
| 240 | + "python": { |
| 241 | + "delete_cmd_postfix": "", |
| 242 | + "delete_cmd_prefix": "del ", |
| 243 | + "library": "var_list.py", |
| 244 | + "varRefreshCmd": "print(var_dic_list())" |
| 245 | + }, |
| 246 | + "r": { |
| 247 | + "delete_cmd_postfix": ") ", |
| 248 | + "delete_cmd_prefix": "rm(", |
| 249 | + "library": "var_list.r", |
| 250 | + "varRefreshCmd": "cat(var_dic_list()) " |
| 251 | + } |
| 252 | + }, |
| 253 | + "types_to_exclude": [ |
| 254 | + "module", |
| 255 | + "function", |
| 256 | + "builtin_function_or_method", |
| 257 | + "instance", |
| 258 | + "_Feature" |
| 259 | + ], |
| 260 | + "window_display": false |
| 261 | + } |
| 262 | + }, |
| 263 | + "nbformat": 4, |
| 264 | + "nbformat_minor": 2 |
| 265 | +} |
0 commit comments