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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
{
"metadata": {
"name": "",
"signature": "sha256:cebcc94856d8ecdf1ae320585ba1942c4e6ccd608bed15be183214bd1392f3a2"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Hour 13: Manipulating String"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 13.1, Page No 210"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"str1=['A',' ','s','t','r','i','n','g',' ','c','o','n','s','t','a','n','t']\n",
"str2= \"Another string constant\"\n",
"\n",
"i=0\n",
"for ch in str1:\n",
" print ch,\n",
"print \"\"\n",
"\n",
"for ch in str2:\n",
" print ch,\n",
"\n",
"print \"\"\n",
"\n",
"#there is no concept of pointer in Python\n",
"ptr_str=\"Assign a string to a pointer\"\n",
"for ele in ptr_str:\n",
" print ele,"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
" A s t r i n g c o n s t a n t \n",
"A n o t h e r s t r i n g c o n s t a n t \n",
"A s s i g n a s t r i n g t o a p o i n t e r\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 13.2, Page No 212"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"str1=['A',' ','s','t','r','i','n','g',' ','c','o','n','s','t','a','n','t']\n",
"str2= \"Another string constant\"\n",
"ptr_str=\"Assign a string to a pointer\"\n",
"\n",
"print \"The length of str1 is: %d bytes\\n\" % len(str1)\n",
"print \"The length of str2 is: %d bytes\\n\" % len(str2)\n",
"print \"The length of the string assigned to a pointer is: %d bytes\" % len(ptr_str)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The length of str1 is: 17 bytes\n",
"\n",
"The length of str2 is: 23 bytes\n",
"\n",
"The length of the string assigned to a pointer is: 28 bytes\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 13.3, Page No 213"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"str1=\"Copy a string\"\n",
"str2=str1\n",
"str3=range(len(str1))\n",
"\n",
"for i in range(len(str1)):\n",
" str3[i]=str1[i]\n",
" \n",
"str3[i]='\\0'\n",
"print \"The content of str2 using strcpy: %s\" % str2\n",
"print \"The contect of str3 without using strcpy: %s\" % str3"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The content of str2 using strcpy: Copy a string\n",
"The contect of str3 without using strcpy: ['C', 'o', 'p', 'y', ' ', 'a', ' ', 's', 't', 'r', 'i', 'n', '\\x00']\n"
]
}
],
"prompt_number": 22
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 13.4, Page No 216"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"str1 = range(80)\n",
"delt=ord('a')-ord('A')\n",
"\n",
"str1 = raw_input(\"Enter a string less than 80 characters: \\n\")\n",
"str1 = list(str1)\n",
"i=0\n",
"while(i!=len(str1)):\n",
" if((str1[i]>='a') and (str1[i]<='z')):\n",
" str1[i] = chr(ord(str1[i])-delt)\n",
" i=i+1\n",
"print \"The entered string is (in uppercase)\\n\"\n",
"print \"\".join(str1)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a string less than 80 characters: \n",
"This is a test.\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The entered string is (in uppercase)\n",
"\n",
"THIS IS A TEST.\n"
]
}
],
"prompt_number": 60
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 13.5, Page No 218"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"x=int(raw_input(\"Enter integer:\"))\n",
"y=int(raw_input(\"Enter integer:\"))\n",
"z=float(raw_input(\"Enter a floating-point number:\"))\n",
"str1=raw_input(\"Enter a string:\")\n",
"print \"Here are what you've entered\"\n",
"print \"%d \" % x,\n",
"print \"%d \" % y\n",
"print \"%f \" % z\n",
"print \"%s \" % str1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter integer:10\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter integer:12345\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a floating-point number:1.234567\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"stream": "stdout",
"text": [
"Enter a string:Test\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Here are what you've entered\n",
"10 12345 \n",
"1.234567 \n",
"Test \n"
]
}
],
"prompt_number": 5
}
],
"metadata": {}
}
]
}
|