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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
{
"metadata": {
"name": "",
"signature": "sha256:3323f7a9a077b84fffdd30478d4b0d0b7f4032b86670a58b5b333d42bee04f15"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Chapter 10: Strings<h1> "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 10.3, Page Number: 10.3<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"st='Programming'\n",
"print 'The string is %s' %st"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The string is Programming\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 10.4, Page Number: 10.4<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"st='Hello World'\n",
"print 'The line is :'\n",
"print st"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The line is :\n",
"Hello World\n"
]
}
],
"prompt_number": 5
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 10.6, Page Number: 10.5<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"strin='Welcome to python'\n",
"for i in strin:\n",
" print i,"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"W e l c o m e t o p y t h o n\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 10.7, Page Number: 10.6<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"str1='Programming'\n",
"len1=len(str1)\n",
"print 'The length of the string is ',len1\n",
"\n",
"str2=str1\n",
"print 'First string is %s and copied string is %s' %(str1,str2)\n",
"\n",
"str3='Computer'\n",
"\n",
"if str1==str3:\n",
" print 'Both strings are equal'\n",
"elif str1<str2:\n",
" print 'First string is lesser than second string'\n",
"else:\n",
" print 'First string is greater than second string'\n",
" \n",
"tempstr=' with C'\n",
"str1=str1+tempstr\n",
"print 'The concated string is ',str1\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The length of the string is 11\n",
"First string is Programming and copied string is Programming\n",
"First string is greater than second string\n",
"The concated string is Programming with C\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 10.8,Page Number: 10.7<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"def strlength(str1):\n",
" count=0\n",
" for i in str1:\n",
" count+=1\n",
" \n",
" return count\n",
"def strcopy(src):\n",
" dst=[]\n",
" for i in src:\n",
" dst.append(i)\n",
" \n",
" dst=''.join(dst)\n",
" \n",
" return dst\n",
"\n",
"\n",
"str1='New Delhi'\n",
"len1=strlength(str1)\n",
"print 'The length of the string is ',len1\n",
"\n",
"str2=strcopy(str1)\n",
"print 'First string is %s and copied string is %s' %(str1,str2)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The length of the string is 9\n",
"First string is New Delhi and copied string is New Delhi\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 10.9,Page Number: 10.9<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"def strcompare(str1,str2):\n",
" \n",
" len1=len(str1)\n",
" len2=len(str2)\n",
" \n",
" if len1<len2:\n",
" length=len1\n",
" else:\n",
" length=len2\n",
" \n",
" for i in xrange(0,length):\n",
" if str1[i]<str2[i]:\n",
" return -1\n",
" elif str1[i]>str2[i]:\n",
" return 1\n",
" \n",
" return 0\n",
"\n",
"str1='Programming'\n",
"str2='Computer'\n",
"status=strcompare(str1,str2)\n",
"if status==-1:\n",
" print 'First string is lesser than second string'\n",
"elif status==1:\n",
" print 'First string is greater than second string'\n",
"else:\n",
" print 'Both strings ae equal'\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"First string is greater than second string\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 10.10, Page Number: 10.10<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"def leftconcat(dst,src):\n",
" dst=src+dst\n",
" return dst\n",
"\n",
"def rightconcat(dst,src):\n",
" dst=dst+src\n",
" return dst\n",
"\n",
"str1='Hello'\n",
"str2='Friends'\n",
"\n",
"tempstr=leftconcat(str2,str1)\n",
"print 'The first string after left concatenation becomes ', tempstr\n",
"\n",
"tempstr=rightconcat(str2,str1)\n",
"print 'The first string after right concatenation becomes', tempstr\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The first string after left concatenation becomes HelloFriends\n",
"The first string after right concatenation becomes FriendsHello\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 10.11,Page Numbr: 10.12<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"str1='All good boys have bread'\n",
"count=0\n",
"\n",
"for i in str1:\n",
" if i=='a' or i=='e'or i=='i' or i=='o' or i=='u' or i=='A' or i=='E' or i=='I' or i=='O' or i=='U' :\n",
" count+=1\n",
" \n",
"print 'Total number of vowels in a given text are ',count"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Total number of vowels in a given text are 8\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 10.12, Page Number: 10.13<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"ch1='A'\n",
"ch2=ord(ch1)+3\n",
"print chr(ch2)\n",
"\n",
"ch1=chr(ord(ch1)+1)\n",
"print ch1\n",
"\n",
"print ord('a')\n",
"print ord('l')\n",
"\n",
"val=ord(ch1)*ch2\n",
"print val\n",
"\n",
"print chr(100)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"D\n",
"B\n",
"97\n",
"108\n",
"4488\n",
"d\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 10.13, Page Number: 10.13<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"text='I am studying 6 Theory Papers & 4 practicals'\n",
"\n",
"len1=len(text)\n",
"text=list(text)\n",
"for i in xrange(0,len1):\n",
" if text[i]>='a' and text[i]<='z':\n",
" text[i]=chr(ord(text[i])+ord('A')-ord('a'))\n",
" \n",
" \n",
"text=''.join(text)\n",
"\n",
"print 'The text after converting lowercase alphabets to uppercase is '\n",
"print text"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The text after converting lowercase alphabets to uppercase is \n",
"I AM STUDYING 6 THEORY PAPERS & 4 PRACTICALS\n"
]
}
],
"prompt_number": 13
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 10.14, Page Number: 10.14<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"text='The programming is a systematic process'\n",
"substr='pro'\n",
"text_len=len(text)\n",
"sub_len=len(substr)\n",
"text=list(text)\n",
"substr=list(substr)\n",
"\n",
"for i in xrange(0,text_len-sub_len+1):\n",
" \n",
" for j in xrange(0,sub_len):\n",
" \n",
" if text[i+j]==substr[j]:\n",
" continue\n",
" else:\n",
" break\n",
" \n",
" if j==sub_len-1:\n",
" print 'The substring is present from subscript %d onwards' %i\n",
" \n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"The substring is present from subscript 4 onwards\n",
"The substring is present from subscript 32 onwards\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h3>Example 10.15,Page number: 10.15<h3>"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"\n",
"\n",
"def reorder(x):\n",
"\n",
" n=len(x)\n",
" for item in range(0,n-1):\n",
" for i in range(item+1,n):\n",
" if x[item]>x[i]:\n",
" temp=x[item]\n",
" x[item]=x[i]\n",
" x[i]=temp\n",
"\n",
"\n",
" return\n",
"\n",
"x=['PACIFIC','ATLANTIC','INDIAN','CARIBBEAN','BERING','BLACK','RED','NORTH','BALTIC','CASPIAN']\n",
"print 'Original list of strings :\\n\\n'\n",
"\n",
"for i in x:\n",
" print \"String : \",i\n",
"\n",
"reorder(x)\n",
"\n",
"print \"\\nReodered list of strings : \\n\\n\"\n",
"\n",
"for i in x:\n",
" print \"String : \",i\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Original list of strings :\n",
"\n",
"\n",
"String : PACIFIC\n",
"String : ATLANTIC\n",
"String : INDIAN\n",
"String : CARIBBEAN\n",
"String : BERING\n",
"String : BLACK\n",
"String : RED\n",
"String : NORTH\n",
"String : BALTIC\n",
"String : CASPIAN\n",
"\n",
"Reodered list of strings : \n",
"\n",
"\n",
"String : ATLANTIC\n",
"String : BALTIC\n",
"String : BERING\n",
"String : BLACK\n",
"String : CARIBBEAN\n",
"String : CASPIAN\n",
"String : INDIAN\n",
"String : NORTH\n",
"String : PACIFIC\n",
"String : RED\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
}
],
"metadata": {}
}
]
}
|