|
4848 | 4848 | }, |
4849 | 4849 | "source": [ |
4850 | 4850 | "These methods help you locate substrings within a string, returning their position or checking for their presence:\n", |
4851 | | - "* `index()` - returns `-1` if the substring is not found\n", |
4852 | | - "* `find()` - raises a `ValueError` if the substring is not found" |
| 4851 | + "* `index()`\n", |
| 4852 | + "* `find()`" |
4853 | 4853 | ] |
4854 | 4854 | }, |
4855 | 4855 | { |
|
4886 | 4886 | "print(line.find('wombat'))" |
4887 | 4887 | ] |
4888 | 4888 | }, |
4889 | | - { |
4890 | | - "cell_type": "markdown", |
4891 | | - "id": "714fc569", |
4892 | | - "metadata": { |
4893 | | - "editable": false, |
4894 | | - "slideshow": { |
4895 | | - "slide_type": "" |
4896 | | - } |
4897 | | - }, |
4898 | | - "source": [ |
4899 | | - "The key difference is how they handle missing substrings - `find()` returns `-1` while `index()` raises an exception:" |
4900 | | - ] |
4901 | | - }, |
4902 | | - { |
4903 | | - "cell_type": "code", |
4904 | | - "execution_count": null, |
4905 | | - "id": "fc5a28ef-8a31-434a-a653-1662cc274945", |
4906 | | - "metadata": { |
4907 | | - "editable": true, |
4908 | | - "slideshow": { |
4909 | | - "slide_type": "" |
4910 | | - }, |
4911 | | - "tags": [] |
4912 | | - }, |
4913 | | - "outputs": [], |
4914 | | - "source": [ |
4915 | | - "try:\n", |
4916 | | - " print(line.index('wombat'))\n", |
4917 | | - "except ValueError:\n", |
4918 | | - " print(\"A wombat isn't mentioned in the text\")" |
4919 | | - ] |
4920 | | - }, |
4921 | 4889 | { |
4922 | 4890 | "cell_type": "markdown", |
4923 | 4891 | "id": "40d4c38b-9a07-4e49-a9b3-003dd13f129d", |
|
5106 | 5074 | "print(f\"Cleaned: '{email}'\")" |
5107 | 5075 | ] |
5108 | 5076 | }, |
5109 | | - { |
5110 | | - "cell_type": "markdown", |
5111 | | - "id": "2fd63835", |
5112 | | - "metadata": { |
5113 | | - "editable": false, |
5114 | | - "slideshow": { |
5115 | | - "slide_type": "slide" |
5116 | | - } |
5117 | | - }, |
5118 | | - "source": [ |
5119 | | - "These methods work with any string and don't raise exceptions, but you may want to validate the results." |
5120 | | - ] |
5121 | | - }, |
5122 | | - { |
5123 | | - "cell_type": "code", |
5124 | | - "execution_count": null, |
5125 | | - "id": "8f1ceba1", |
5126 | | - "metadata": {}, |
5127 | | - "outputs": [], |
5128 | | - "source": [ |
5129 | | - "# Transforming methods don't raise exceptions, but you can validate transformed results\n", |
5130 | | - "def process_username(username):\n", |
5131 | | - " \"\"\"Process and validate a username.\"\"\"\n", |
5132 | | - " if not isinstance(username, str):\n", |
5133 | | - " raise TypeError(f\"Username must be a string, got {type(username).__name__}\")\n", |
5134 | | - " \n", |
5135 | | - " # Transform: strip whitespace and convert to lowercase\n", |
5136 | | - " processed = username.strip().lower()\n", |
5137 | | - " \n", |
5138 | | - " # Validate the result\n", |
5139 | | - " if not processed:\n", |
5140 | | - " raise ValueError(\"Username cannot be empty after processing\")\n", |
5141 | | - " \n", |
5142 | | - " if len(processed) < 3:\n", |
5143 | | - " raise ValueError(f\"Username must be at least 3 characters, got {len(processed)}\")\n", |
5144 | | - " \n", |
5145 | | - " return processed\n", |
5146 | | - "\n", |
5147 | | - "# Valid case\n", |
5148 | | - "try:\n", |
5149 | | - " result = process_username(\" JohnDoe \")\n", |
5150 | | - " print(f\"\u2713 Processed username: '{result}'\")\n", |
5151 | | - "except ValueError as e:\n", |
5152 | | - " print(f\"Error: {e}\")\n", |
5153 | | - "\n", |
5154 | | - "# Invalid case\n", |
5155 | | - "try:\n", |
5156 | | - " result = process_username(\" AB \")\n", |
5157 | | - " print(f\"\u2713 Processed username: '{result}'\")\n", |
5158 | | - "except ValueError as e:\n", |
5159 | | - " print(f\"Error: {e}\")" |
5160 | | - ] |
5161 | | - }, |
5162 | 5077 | { |
5163 | 5078 | "cell_type": "markdown", |
5164 | 5079 | "id": "6cf8076f-8f50-49ae-b558-39336c323880", |
|
5258 | 5173 | "print(\"\\n\".join(line_list))" |
5259 | 5174 | ] |
5260 | 5175 | }, |
5261 | | - { |
5262 | | - "cell_type": "markdown", |
5263 | | - "id": "0deaf239", |
5264 | | - "metadata": { |
5265 | | - "editable": false, |
5266 | | - "slideshow": { |
5267 | | - "slide_type": "" |
5268 | | - } |
5269 | | - }, |
5270 | | - "source": [ |
5271 | | - "`join()` will raise a `TypeError` if the iterable contains non-string elements" |
5272 | | - ] |
5273 | | - }, |
5274 | | - { |
5275 | | - "cell_type": "code", |
5276 | | - "execution_count": null, |
5277 | | - "id": "225bdd40", |
5278 | | - "metadata": { |
5279 | | - "editable": true, |
5280 | | - "slideshow": { |
5281 | | - "slide_type": "" |
5282 | | - }, |
5283 | | - "tags": [] |
5284 | | - }, |
5285 | | - "outputs": [], |
5286 | | - "source": [ |
5287 | | - "flags = [16384,1048576]\n", |
5288 | | - "\n", |
5289 | | - "for i in range(len(flags)):\n", |
5290 | | - " flags_str = \"+\".join(flags[i])\n", |
5291 | | - "#flags_str = \"+\".join(str(flag) for flag in flags)\n", |
5292 | | - "#print(flags_str)" |
5293 | | - ] |
5294 | | - }, |
5295 | 5176 | { |
5296 | 5177 | "cell_type": "markdown", |
5297 | 5178 | "id": "a965b5d9-59f4-4ed5-b7e9-89f5f0bcf60f", |
|
0 commit comments