|
2125 | 2125 | "id": "sustainable-brunswick" |
2126 | 2126 | }, |
2127 | 2127 | "source": [ |
2128 | | - "## Activity: A list of strings is given, a word is given too\n", |
| 2128 | + "## Activity 26: String Concatenation\n", |
2129 | 2129 | "\n", |
2130 | | - "- Write a Python code that inserts the word between the list elements and combine them as one single string\n", |
| 2130 | + "One common string operation is joining two or more strings together, to form a larger one - this is known as *concatenation*. \n", |
2131 | 2131 | "\n", |
2132 | | - "- Example: string1 = ['Milad', 'Amir', 'Toutounchian'] is given, 'Test' is given two. Your code should return MiladTestAmirTestToutounchian" |
| 2132 | + "Let's challenge your string concatenation skills: for example, say you are given a list of strings, `strings`, and another string that is just a single word - `word`.\n", |
| 2133 | + "\n", |
| 2134 | + "How would you implement Python code to concatenate the elements in `strings` together, with the `word` string placed in-between every pair of elements in the output?" |
2133 | 2135 | ], |
2134 | 2136 | "id": "sustainable-brunswick" |
2135 | 2137 | }, |
| 2138 | + { |
| 2139 | + "cell_type": "markdown", |
| 2140 | + "metadata": { |
| 2141 | + "id": "JGtlRMQZKzXX" |
| 2142 | + }, |
| 2143 | + "source": [ |
| 2144 | + "### Example Input\n", |
| 2145 | + "\n", |
| 2146 | + "```\n", |
| 2147 | + "Input:\n", |
| 2148 | + "strings = [\n", |
| 2149 | + " 'How was work?', \n", |
| 2150 | + " 'day, ', \n", |
| 2151 | + " 'dollar.'\n", |
| 2152 | + "]\n", |
| 2153 | + "word = ' Another ' \n", |
| 2154 | + "\n", |
| 2155 | + "Output:\n", |
| 2156 | + "How was work? Another day, Another dollar\n", |
| 2157 | + "\n", |
| 2158 | + "Explanation:\n", |
| 2159 | + "The word ' Another ' is placed in between the 1st and 2nd elements in the list, as well as the 2nd and 3rd.\n", |
| 2160 | + "```" |
| 2161 | + ], |
| 2162 | + "id": "JGtlRMQZKzXX" |
| 2163 | + }, |
2136 | 2164 | { |
2137 | 2165 | "cell_type": "code", |
2138 | 2166 | "metadata": { |
2139 | 2167 | "id": "hollow-passing" |
2140 | 2168 | }, |
2141 | 2169 | "source": [ |
2142 | | - "string1 = ['Milad', 'Amir', 'Toutounchian']" |
| 2170 | + "strings = ['How was work?', 'day,', 'dollar.']\n", |
| 2171 | + "word = ' Another ' " |
2143 | 2172 | ], |
2144 | 2173 | "id": "hollow-passing", |
2145 | | - "execution_count": null, |
| 2174 | + "execution_count": 17, |
2146 | 2175 | "outputs": [] |
2147 | 2176 | }, |
2148 | 2177 | { |
2149 | | - "cell_type": "code", |
| 2178 | + "cell_type": "markdown", |
2150 | 2179 | "metadata": { |
2151 | | - "id": "antique-estonia", |
2152 | | - "outputId": "7a4d91e0-23ce-4f7a-b753-c1561002c8d4" |
| 2180 | + "id": "OL87Oy76Nqk6" |
2153 | 2181 | }, |
2154 | 2182 | "source": [ |
2155 | | - "given_word = 'Test'\n", |
2156 | | - "S = ''\n", |
2157 | | - "for n, string in enumerate(string1):\n", |
2158 | | - " if n != (len(string1) - 1):\n", |
2159 | | - " S = S + string + given_word\n", |
2160 | | - " else:\n", |
2161 | | - " S = S + string\n", |
| 2183 | + "### Solution 1: Using String Addition\n", |
2162 | 2184 | "\n", |
2163 | | - "print(S)" |
| 2185 | + "Strings in Python are just another kind of iterable. Therefore, Python lets us concatenate strings with the `+` operator, and that itself will create a larger string:" |
| 2186 | + ], |
| 2187 | + "id": "OL87Oy76Nqk6" |
| 2188 | + }, |
| 2189 | + { |
| 2190 | + "cell_type": "code", |
| 2191 | + "metadata": { |
| 2192 | + "id": "antique-estonia" |
| 2193 | + }, |
| 2194 | + "source": [ |
| 2195 | + "def concatenate_strings_1(strings, word):\n", |
| 2196 | + " # A: init the output string\n", |
| 2197 | + " message = ''\n", |
| 2198 | + " # B: construct the message \n", |
| 2199 | + " for index, string in enumerate(strings):\n", |
| 2200 | + " # add the next string to the output\n", |
| 2201 | + " message = message + string\n", |
| 2202 | + " # also add the word (if we haven't reached the last element yet)\n", |
| 2203 | + " if index != len(strings) - 1:\n", |
| 2204 | + " message = message + word\n", |
| 2205 | + " # C: display the output string\n", |
| 2206 | + " print(message)" |
2164 | 2207 | ], |
2165 | 2208 | "id": "antique-estonia", |
2166 | | - "execution_count": null, |
| 2209 | + "execution_count": 20, |
| 2210 | + "outputs": [] |
| 2211 | + }, |
| 2212 | + { |
| 2213 | + "cell_type": "markdown", |
| 2214 | + "metadata": { |
| 2215 | + "id": "D0KA60YVPKMy" |
| 2216 | + }, |
| 2217 | + "source": [ |
| 2218 | + "#### Test Out Solution 1" |
| 2219 | + ], |
| 2220 | + "id": "D0KA60YVPKMy" |
| 2221 | + }, |
| 2222 | + { |
| 2223 | + "cell_type": "code", |
| 2224 | + "metadata": { |
| 2225 | + "colab": { |
| 2226 | + "base_uri": "https://localhost:8080/" |
| 2227 | + }, |
| 2228 | + "id": "35SzmDUOPL6C", |
| 2229 | + "outputId": "b1ebd67c-44a9-4ebb-d44c-4fa8fb9e8f24" |
| 2230 | + }, |
| 2231 | + "source": [ |
| 2232 | + "concatenate_strings_1(strings, word)" |
| 2233 | + ], |
| 2234 | + "id": "35SzmDUOPL6C", |
| 2235 | + "execution_count": 21, |
2167 | 2236 | "outputs": [ |
2168 | 2237 | { |
2169 | 2238 | "output_type": "stream", |
2170 | 2239 | "text": [ |
2171 | | - "MiladTestAmirTestToutounchian\n" |
| 2240 | + "How was work? Another day, Another dollar.\n" |
2172 | 2241 | ], |
2173 | 2242 | "name": "stdout" |
2174 | 2243 | } |
2175 | 2244 | ] |
2176 | 2245 | }, |
| 2246 | + { |
| 2247 | + "cell_type": "markdown", |
| 2248 | + "metadata": { |
| 2249 | + "id": "GjUaxcs_Qh08" |
| 2250 | + }, |
| 2251 | + "source": [ |
| 2252 | + "### Solution 2: Using `str.join()`\n", |
| 2253 | + "\n", |
| 2254 | + "The `str` type in Python also comes with a method to make concatenation much easier: `str.join()`.\n", |
| 2255 | + "\n", |
| 2256 | + "This method is preferred to using addition. Not only is it more Pythonic, `str.join()` is also costs less computational resources. You may read more about its syntax in the [Python documenation](https://docs.python.org/3/library/stdtypes.html#str.join).\n", |
| 2257 | + "\n", |
| 2258 | + "Please see below an example of how this works:" |
| 2259 | + ], |
| 2260 | + "id": "GjUaxcs_Qh08" |
| 2261 | + }, |
2177 | 2262 | { |
2178 | 2263 | "cell_type": "code", |
2179 | 2264 | "metadata": { |
2180 | | - "id": "automotive-entrepreneur", |
2181 | | - "outputId": "34705a9d-471d-4823-d60e-3db643d70408" |
| 2265 | + "id": "automotive-entrepreneur" |
2182 | 2266 | }, |
2183 | 2267 | "source": [ |
2184 | | - "## easier way in Python\n", |
2185 | | - "print(given_word.join(string1))" |
| 2268 | + "def concatenate_strings_2(strings, word): \n", |
| 2269 | + " # construct + display the output string\n", |
| 2270 | + " print(word.join(strings))" |
2186 | 2271 | ], |
2187 | 2272 | "id": "automotive-entrepreneur", |
2188 | | - "execution_count": null, |
| 2273 | + "execution_count": 26, |
| 2274 | + "outputs": [] |
| 2275 | + }, |
| 2276 | + { |
| 2277 | + "cell_type": "markdown", |
| 2278 | + "metadata": { |
| 2279 | + "id": "ZgCJYZ8hS_OK" |
| 2280 | + }, |
| 2281 | + "source": [ |
| 2282 | + "#### Test Out Solution 2" |
| 2283 | + ], |
| 2284 | + "id": "ZgCJYZ8hS_OK" |
| 2285 | + }, |
| 2286 | + { |
| 2287 | + "cell_type": "code", |
| 2288 | + "metadata": { |
| 2289 | + "colab": { |
| 2290 | + "base_uri": "https://localhost:8080/" |
| 2291 | + }, |
| 2292 | + "id": "h3_CUksUTFHm", |
| 2293 | + "outputId": "59ddb93f-233d-4db7-c9e4-120afc4d2a1a" |
| 2294 | + }, |
| 2295 | + "source": [ |
| 2296 | + "concatenate_strings_2(strings, word)" |
| 2297 | + ], |
| 2298 | + "id": "h3_CUksUTFHm", |
| 2299 | + "execution_count": 27, |
2189 | 2300 | "outputs": [ |
2190 | 2301 | { |
2191 | 2302 | "output_type": "stream", |
2192 | 2303 | "text": [ |
2193 | | - "MiladTestAmirTestToutounchian\n" |
| 2304 | + "How was work? Another day, Another dollar.\n" |
2194 | 2305 | ], |
2195 | 2306 | "name": "stdout" |
2196 | 2307 | } |
|
0 commit comments