Skip to content

Commit b08e609

Browse files
Edit C0-A26
1 parent 8dd6489 commit b08e609

File tree

1 file changed

+135
-24
lines changed

1 file changed

+135
-24
lines changed

chapter0-exercises/chapter_0_part2.ipynb

Lines changed: 135 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,72 +2125,183 @@
21252125
"id": "sustainable-brunswick"
21262126
},
21272127
"source": [
2128-
"## Activity: A list of strings is given, a word is given too\n",
2128+
"## Activity 26: String Concatenation\n",
21292129
"\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",
21312131
"\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?"
21332135
],
21342136
"id": "sustainable-brunswick"
21352137
},
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+
},
21362164
{
21372165
"cell_type": "code",
21382166
"metadata": {
21392167
"id": "hollow-passing"
21402168
},
21412169
"source": [
2142-
"string1 = ['Milad', 'Amir', 'Toutounchian']"
2170+
"strings = ['How was work?', 'day,', 'dollar.']\n",
2171+
"word = ' Another ' "
21432172
],
21442173
"id": "hollow-passing",
2145-
"execution_count": null,
2174+
"execution_count": 17,
21462175
"outputs": []
21472176
},
21482177
{
2149-
"cell_type": "code",
2178+
"cell_type": "markdown",
21502179
"metadata": {
2151-
"id": "antique-estonia",
2152-
"outputId": "7a4d91e0-23ce-4f7a-b753-c1561002c8d4"
2180+
"id": "OL87Oy76Nqk6"
21532181
},
21542182
"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",
21622184
"\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)"
21642207
],
21652208
"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,
21672236
"outputs": [
21682237
{
21692238
"output_type": "stream",
21702239
"text": [
2171-
"MiladTestAmirTestToutounchian\n"
2240+
"How was work? Another day, Another dollar.\n"
21722241
],
21732242
"name": "stdout"
21742243
}
21752244
]
21762245
},
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+
},
21772262
{
21782263
"cell_type": "code",
21792264
"metadata": {
2180-
"id": "automotive-entrepreneur",
2181-
"outputId": "34705a9d-471d-4823-d60e-3db643d70408"
2265+
"id": "automotive-entrepreneur"
21822266
},
21832267
"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))"
21862271
],
21872272
"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,
21892300
"outputs": [
21902301
{
21912302
"output_type": "stream",
21922303
"text": [
2193-
"MiladTestAmirTestToutounchian\n"
2304+
"How was work? Another day, Another dollar.\n"
21942305
],
21952306
"name": "stdout"
21962307
}

0 commit comments

Comments
 (0)