-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1String-Indexing.html
More file actions
154 lines (131 loc) · 5.47 KB
/
1String-Indexing.html
File metadata and controls
154 lines (131 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<h2 id="learning-objectives">Learning Objectives:</h2>
<ul>
<li>Subscript strings </li>
<li>Combine strings and/or string literals </li>
</ul>
<h3 id="trouble-problem-from-f17-1066-order-out-of-239">Trouble problem from F17: 10.6.6) Order out of (23.9%)</h3>
<h3 id="problem-in-s18-166-order-out-of">Problem in S18: 16.6) Order out of</h3>
<h2 id="problem-given-strings-assigment951-and-assignment952-shown-below-use-string-indexing-to-extract-the-due-dates-of-each-assignment-and-print-the-due-dates-separated-by-a-comma-and-a-space">Problem: Given strings assigment_1 and assignment_2, shown below, use string indexing to extract the due dates of each assignment and print the due dates, separated by a comma and a space.</h2>
<pre class="codehilite"><code>assignment_1 = "Maze Game,Jan. 26,10pts"
assignment_2 = "Types,Feb. 2,10pts"</code></pre>
<h3 id="step-0-understanding-the-problem">Step 0: Understanding the problem.</h3>
<p>The idea of this problem is that you are given two strings, assignment_1 and assignment_2, each representing some details about a particular assignment. The goal is to use string indexing to extract the due date from each assignment string and print the due dates as a neatly formatted list. So the expected output is </p>
<pre class="codehilite"><code>Jan. 26, Feb. 2</code></pre>
<h3 id="step-1-find-the-indexes-of-each-due-date">Step 1: Find the indexes of each due date.</h3>
<p><table><br />
<tr><br />
<th>0</th><br />
<th>1</th><br />
<th>2</th><br />
<th>3</th><br />
<th>4</th><br />
<th>5</th><br />
<th>6</th><br />
<th>7</th><br />
<th>8</th><br />
<th>9</th><br />
<th>10</th><br />
<th>11</th><br />
<th>12</th><br />
<th>13</th><br />
<th>14</th><br />
<th>15</th><br />
<th>16</th><br />
<th>17</th><br />
<th>18</th><br />
<th>19</th><br />
<th>20</th><br />
<th>21</th><br />
<th>22</th><br />
</tr><br />
<tr><br />
<td>M</td><br />
<td>a</td><br />
<td>z</td><br />
<td>e</td><br />
<td> </td><br />
<td>G</td><br />
<td>a</td><br />
<td>m</td><br />
<td>e</td><br />
<td>,</td><br />
<td>J</td><br />
<td>a</td><br />
<td>n</td><br />
<td>.</td><br />
<td> </td><br />
<td>2</td><br />
<td>6</td><br />
<td>,</td><br />
<td>1</td><br />
<td>0</td><br />
<td>p</td><br />
<td>t</td><br />
<td>s</td><br />
</tr><br />
</table></p>
<p>We can see that the due date for assignment_1 (Jan. 26) starts at index 10 and ends at index 16. Therefore to include the entire date with string indexing we need to put </p>
<pre class="codehilite"><code>assignment_1[10:17]</code></pre>
<p>because python stops at the character before the index given by the second number. </p>
<p>Doing the same thing for assignment_2 we can see we need to use </p>
<pre class="codehilite"><code>assignment_2[6:11]</code></pre>
<table>
<tr>
<th>0</th>
<th>1</th>
<th>2</th>
<th>3</th>
<th>4</th>
<th>5</th>
<th>6</th>
<th>7</th>
<th>8</th>
<th>9</th>
<th>10</th>
<th>11</th>
<th>12</th>
<th>13</th>
<th>14</th>
<th>15</th>
<th>16</th>
<th>17</th>
</tr>
<tr>
<td>T</td>
<td>y</td>
<td>p</td>
<td>e</td>
<td>s</td>
<td>,</td>
<td>F</td>
<td>e</td>
<td>b</td>
<td>.</td>
<td> </td>
<td>2</td>
<td>,</td>
<td>1</td>
<td>0</td>
<td>p</td>
<td>t</td>
<td>s</td>
</tr>
</table>
<h3 id="step-2-create-the-formatted-with-a-comma-and-space-due-date-string">Step 2: Create the formatted (with a comma and space) due date string.</h3>
<p>We know we can use the + operator to concatenate (combine) strings. So </p>
<pre class="codehilite"><code>assignment_1[10:17] + assignment_2[6:11]</code></pre>
<p>gives us</p>
<pre class="codehilite"><code>Jan. 26Feb. 2</code></pre>
<p>But this isn’t exactly what we want. We can also concatenate string literals using the + operator. String literals are any string values in quotation marks. So <code>assignment_1</code> is a variable, while <code>"Maze Game,Jan. 26,10pts"</code> is the string literal value we store in the variable. So with that in mind, we can use </p>
<pre class="codehilite"><code>assignment_1[10:17] + ", " + assignment_2[6:11] #Notice the space!</code></pre>
<p>which gives us</p>
<pre class="codehilite"><code>Jan. 26, Feb. 2</code></pre>
<h3 id="step-3-print-out-the-result">Step 3: Print out the result.</h3>
<p>To display the string created in Step 2, we can store it in a variable like this: </p>
<pre class="codehilite"><code>due_dates = assignment_1[10:17] + ", " + assignment_2[6:11]</code></pre>
<p>then print out the variable we created: </p>
<pre class="codehilite"><code>print(due_dates)</code></pre>
<p>or, we can simply print the statement from Step 2 directly by putting it in the print statement. </p>
<pre class="codehilite"><code>print(assignment_1[10:17] + ", " + assignment_2[6:11])</code></pre>
<h3 id="solution">Solution:</h3>
<pre class="codehilite"><code>print(assignment_1[10:17] + ", " + assignment_2[6:11])</code></pre>