|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "a1965c4d-73db-4b51-9a33-50ca416827a2", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## String Methods" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "markdown", |
| 13 | + "id": "d5d0da4e-3ce4-49af-97e3-256baaf685bf", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "### Repeating/Replicating Strings\n", |
| 17 | + "In Python, we can repeat or replicate strings using the multiplication operator *. The multiplication operator takes two operands: a string and an integer. When we multiply a string by an integer, Python returns a new string that contains the original string repeated the specified number of times." |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": 1, |
| 23 | + "id": "2e3dbd05-0f7c-48bd-b21c-6200e3166fb4", |
| 24 | + "metadata": { |
| 25 | + "tags": [] |
| 26 | + }, |
| 27 | + "outputs": [ |
| 28 | + { |
| 29 | + "name": "stdout", |
| 30 | + "output_type": "stream", |
| 31 | + "text": [ |
| 32 | + "GeoNextGeoNextGeoNext\n" |
| 33 | + ] |
| 34 | + } |
| 35 | + ], |
| 36 | + "source": [ |
| 37 | + "name = \"GeoNext\"\n", |
| 38 | + "print(name * 3)" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "markdown", |
| 43 | + "id": "cf55298d-20a5-4ea7-8f0f-4f3c50edc803", |
| 44 | + "metadata": {}, |
| 45 | + "source": [ |
| 46 | + "### String Slicing\n", |
| 47 | + "String slicing is the process of extracting a part of a string. In Python, we can slice a string using two methods.\n", |
| 48 | + "* Extending Indexing\n", |
| 49 | + "* slice() Constructor" |
| 50 | + ] |
| 51 | + }, |
| 52 | + { |
| 53 | + "cell_type": "markdown", |
| 54 | + "id": "b1bc25c6-244a-48b6-96ae-d699a72403d0", |
| 55 | + "metadata": {}, |
| 56 | + "source": [ |
| 57 | + "#### Extending Indexing\n", |
| 58 | + "In Python, we can slice a string using the indexing operator []. The indexing operator takes one or two arguments: the index of the character we want to extract, or the start and end indices of the slice we want to extract." |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": 2, |
| 64 | + "id": "fefe079d-66ba-4445-a065-444e70efc997", |
| 65 | + "metadata": { |
| 66 | + "tags": [] |
| 67 | + }, |
| 68 | + "outputs": [ |
| 69 | + { |
| 70 | + "name": "stdout", |
| 71 | + "output_type": "stream", |
| 72 | + "text": [ |
| 73 | + "N\n", |
| 74 | + "Geo\n", |
| 75 | + "Next\n", |
| 76 | + "Goet\n" |
| 77 | + ] |
| 78 | + } |
| 79 | + ], |
| 80 | + "source": [ |
| 81 | + "name = \"GeoNext\"\n", |
| 82 | + "print(name[3])\n", |
| 83 | + "print(name[0:3])\n", |
| 84 | + "print(name[3:])\n", |
| 85 | + "print(name[::2])" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": 3, |
| 91 | + "id": "7d4b3d55-6ccf-4027-b745-cd5e530fa4f5", |
| 92 | + "metadata": { |
| 93 | + "tags": [] |
| 94 | + }, |
| 95 | + "outputs": [ |
| 96 | + { |
| 97 | + "name": "stdout", |
| 98 | + "output_type": "stream", |
| 99 | + "text": [ |
| 100 | + "txeNoeG\n" |
| 101 | + ] |
| 102 | + } |
| 103 | + ], |
| 104 | + "source": [ |
| 105 | + "# Print string in reverse\n", |
| 106 | + "print(name[::-1])" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "markdown", |
| 111 | + "id": "aeadce5a-afce-4f36-a26e-705d13b993c7", |
| 112 | + "metadata": {}, |
| 113 | + "source": [ |
| 114 | + "#### slice() Constructor\n", |
| 115 | + "Python provides a built-in function called slice() that we can use to create slice objects. Slice objects can then be passed as arguments to the indexing operator to slice a string.<br>\n", |
| 116 | + "**Syntax:**\n", |
| 117 | + "slice(start, stop, step)\n", |
| 118 | + "* start: Optional. An integer that specifies the starting index of the slice. If omitted, it defaults to None, which means the slice starts at the beginning of the string.\n", |
| 119 | + "* stop: Required. An integer that specifies the ending index of the slice. This index is not included in the slice.\n", |
| 120 | + "* step: Optional. An integer that specifies the step size of the slice. If omitted, it defaults to None, which means the slice uses a step size of 1." |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "code", |
| 125 | + "execution_count": 4, |
| 126 | + "id": "472f3bdb-7aa7-4483-af0f-0a40d1371046", |
| 127 | + "metadata": { |
| 128 | + "tags": [] |
| 129 | + }, |
| 130 | + "outputs": [ |
| 131 | + { |
| 132 | + "name": "stdout", |
| 133 | + "output_type": "stream", |
| 134 | + "text": [ |
| 135 | + "Geo\n", |
| 136 | + "Next\n", |
| 137 | + "Goet\n" |
| 138 | + ] |
| 139 | + } |
| 140 | + ], |
| 141 | + "source": [ |
| 142 | + "name = \"GeoNext\"\n", |
| 143 | + "s1 = slice(0, 3)\n", |
| 144 | + "s2 = slice(3, 8)\n", |
| 145 | + "s3 = slice(0, 8, 2)\n", |
| 146 | + "print(name[s1])\n", |
| 147 | + "print(name[s2])\n", |
| 148 | + "print(name[s3])" |
| 149 | + ] |
| 150 | + }, |
| 151 | + { |
| 152 | + "cell_type": "markdown", |
| 153 | + "id": "2e509ab8-882f-4baf-836a-209e07d0a2fd", |
| 154 | + "metadata": {}, |
| 155 | + "source": [ |
| 156 | + "### String Comparison\n", |
| 157 | + "Comparing strings is the process of comparing two or more strings to determine whether they are equal or not. String comparison in Python takes place character by character i.e. each character in the same index, are compared with each other.\n", |
| 158 | + "\n", |
| 159 | + "There are several ways to compare strings in Python. One way is to use the comparison operators such as ==, !=, <, <=, >, and >=. These operators compare the strings lexicographically and return a Boolean value of True or False depending on the comparison result." |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "code", |
| 164 | + "execution_count": 5, |
| 165 | + "id": "f30cbe5c-0d86-4491-ac63-12287c9d6c0b", |
| 166 | + "metadata": { |
| 167 | + "tags": [] |
| 168 | + }, |
| 169 | + "outputs": [ |
| 170 | + { |
| 171 | + "name": "stdout", |
| 172 | + "output_type": "stream", |
| 173 | + "text": [ |
| 174 | + "True\n", |
| 175 | + "True\n", |
| 176 | + "False\n", |
| 177 | + "False\n", |
| 178 | + "True\n", |
| 179 | + "True\n" |
| 180 | + ] |
| 181 | + } |
| 182 | + ], |
| 183 | + "source": [ |
| 184 | + "str1 = \"Krishnagopal\"\n", |
| 185 | + "str2 = \"Krishnagopal\"\n", |
| 186 | + "str3 = \"Halder\"\n", |
| 187 | + "print(str1 == str2) # Checks if two strings are equal or not\n", |
| 188 | + "print (str1 != str3) # Checks if two strings are not equal\n", |
| 189 | + "print(str1 < str3) # Checks if the first string is less than the second string\n", |
| 190 | + "print(str1 <= str3) # Checks if the first string is less than or equal to the second string\n", |
| 191 | + "print(str1 > str3) # Checks if the first string is greater than the second string\n", |
| 192 | + "print(str1 >= str3) # Checks if the first string is greater than or equal to the second string" |
| 193 | + ] |
| 194 | + } |
| 195 | + ], |
| 196 | + "metadata": { |
| 197 | + "kernelspec": { |
| 198 | + "display_name": "Python 3 (ipykernel)", |
| 199 | + "language": "python", |
| 200 | + "name": "python3" |
| 201 | + }, |
| 202 | + "language_info": { |
| 203 | + "codemirror_mode": { |
| 204 | + "name": "ipython", |
| 205 | + "version": 3 |
| 206 | + }, |
| 207 | + "file_extension": ".py", |
| 208 | + "mimetype": "text/x-python", |
| 209 | + "name": "python", |
| 210 | + "nbconvert_exporter": "python", |
| 211 | + "pygments_lexer": "ipython3", |
| 212 | + "version": "3.9.16" |
| 213 | + } |
| 214 | + }, |
| 215 | + "nbformat": 4, |
| 216 | + "nbformat_minor": 5 |
| 217 | +} |
0 commit comments