author | Puneeth Chaganti <punchagan@fossee.in> |
Wed, 10 Nov 2010 10:47:21 +0530 | |
changeset 436 | e62bc810999c |
parent 313 | b9b7bfce773e |
child 513 | c1f89e89d89c |
permissions | -rw-r--r-- |
313
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
1 |
.. Objectives |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
2 |
.. ---------- |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
3 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
4 |
.. At the end of this tutorial, you should know -- |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
5 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
6 |
.. 1. How to define strings |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
7 |
.. #. Different ways of defining a string |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
8 |
.. #. How to concatenate strings |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
9 |
.. #. How to print a string repeatedly |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
10 |
.. #. Accessing individual elements of the string |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
11 |
.. #. Immutability of strings |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
12 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
13 |
.. Prerequisites |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
14 |
.. ------------- |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
15 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
16 |
.. 1. getting started with ipython |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
17 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
18 |
.. Author : Madhu |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
19 |
Internal Reviewer : |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
20 |
External Reviewer : |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
21 |
Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
22 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
23 |
Script |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
24 |
------ |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
25 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
26 |
{{{ Show the slide containing the title }}} |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
27 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
28 |
Hello friends. Welcome to this spoken tutorial on Getting started with |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
29 |
strings. |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
30 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
31 |
{{{ Show the slide containing the outline }}} |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
32 |
|
436
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
33 |
In this tutorial, we will look at what we really mean by strings, how |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
34 |
python supports the use of strings and some of the operations that can |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
35 |
be performed on strings. |
313
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
36 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
37 |
{{{ Shift to terminal and start ipython }}} |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
38 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
39 |
To begin with let us start ipython, by typing:: |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
40 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
41 |
ipython |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
42 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
43 |
on the terminal |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
44 |
|
436
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
45 |
So, what are strings? In Python anything within either single quotes |
313
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
46 |
or double quotes or triple single quotes or triple double quotes are |
436
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
47 |
strings. |
313
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
48 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
49 |
{{{ Type in ipython the following and read them as you type }}}:: |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
50 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
51 |
'This is a string' |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
52 |
"This is a string too' |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
53 |
'''This is a string as well''' |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
54 |
"""This is also a string""" |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
55 |
'p' |
436
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
56 |
"" |
313
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
57 |
|
436
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
58 |
Note that it really doesn't matter how many characters are present in |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
59 |
the string. The last example is a null string or an empty string. |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
60 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
61 |
Having more than one control character to define strings is handy when |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
62 |
one of the control characters itself is part of the string. For |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
63 |
example:: |
313
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
64 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
65 |
"Python's string manipulation functions are very useful" |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
66 |
|
436
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
67 |
By having multiple control characters, we avoid the need for |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
68 |
escaping characters -- in this case the apostrophe. |
313
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
69 |
|
436
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
70 |
The triple quoted strings let us define multi-line strings without |
313
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
71 |
using any escaping. Everything within the triple quotes is a single |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
72 |
string no matter how many lines it extends:: |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
73 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
74 |
"""Having more than one control character to define |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
75 |
strings come as very handy when one of the control |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
76 |
characters itself is part of the string.""" |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
77 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
78 |
We can assign this string to any variable:: |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
79 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
80 |
a = 'Hello, World!' |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
81 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
82 |
Now 'a' is a string variable. String is a collection of characters. In |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
83 |
addition string is an immutable collection. So all the operations that |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
84 |
are applicable to any other immutable collection in Python works on |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
85 |
string as well. So we can add two strings:: |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
86 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
87 |
a = 'Hello' |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
88 |
b = 'World' |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
89 |
c = a + ', ' + b + '!' |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
90 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
91 |
We can add string variables as well as the strings themselves all in |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
92 |
the same statement. The addition operation performs the concatenation |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
93 |
of two strings. |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
94 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
95 |
Similarly we can multiply a string with an integer:: |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
96 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
97 |
a = 'Hello' |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
98 |
a * 5 |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
99 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
100 |
gives another string in which the original string 'Hello' is repeated |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
101 |
5 times. |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
102 |
|
436
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
103 |
Following is an exercise that you must do. |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
104 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
105 |
%% %% Obtain the string ``%% -------------------- %%`` (20 hyphens) |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
106 |
without typing out all the twenty hyphens. |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
107 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
108 |
Please, pause the video here. Do the exercise and then continue. |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
109 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
110 |
:: |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
111 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
112 |
s = "%% " + "-"*20 + " %%" |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
113 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
114 |
Let's now look at accessing individual elements of strings. Since, |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
115 |
strings are collections we can access individual items in the string |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
116 |
using the subscripts:: |
313
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
117 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
118 |
a[0] |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
119 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
120 |
gives us the first character in the string. The indexing starts from 0 |
436
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
121 |
for the first character and goes up to n-1 for the last character. We |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
122 |
can access the strings from the end using negative indices:: |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
123 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
124 |
a[-1] |
313
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
125 |
|
436
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
126 |
gives us the last element of the string and |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
127 |
:: |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
128 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
129 |
a[-2] |
313
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
130 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
131 |
gives us second element from the end of the string |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
132 |
|
436
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
133 |
Following is an exercise that you must do. |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
134 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
135 |
%% %% Given a string, ``s = "Hello World"``, what is the output of:: |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
136 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
137 |
s[-5] |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
138 |
s[-10] |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
139 |
s[-15] |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
140 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
141 |
Please, pause the video here. Do the exercise and then continue. |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
142 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
143 |
:: |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
144 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
145 |
s[-5] |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
146 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
147 |
gives us 'W' |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
148 |
:: |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
149 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
150 |
s[-10] |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
151 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
152 |
gives us 'e' and |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
153 |
:: |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
154 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
155 |
s[-15] |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
156 |
|
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
157 |
gives us an ``IndexError``, as should be expected, since the string |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
158 |
given to us is only 11 characters long. |
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
159 |
|
313
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
160 |
Let us attempt to change one of the characters in a string:: |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
161 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
162 |
a = 'hello' |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
163 |
a[0] = 'H' |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
164 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
165 |
As said earlier, strings are immutable. We cannot manipulate the |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
166 |
string. Although there are some methods which let us to manipulate the |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
167 |
strings. We will look at them in the advanced session on strings. In |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
168 |
addition to the methods that let us manipulate the strings we have |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
169 |
methods like split which lets us break the string on the specified |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
170 |
separator, the join method which lets us combine the list of strings |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
171 |
into a single string based on the specified separator. |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
172 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
173 |
{{{ Show summary slide }}} |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
174 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
175 |
This brings us to the end of another session. In this tutorial session |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
176 |
we learnt |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
177 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
178 |
* How to define strings |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
179 |
* Different ways of defining a string |
436
e62bc810999c
Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
313
diff
changeset
|
180 |
* String concatenation and repetition |
313
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
181 |
* Accessing individual elements of the string |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
182 |
* Immutability of strings |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
183 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
184 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
185 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
186 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
187 |
|
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
188 |
Hope you have enjoyed and found it useful. |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
189 |
Thank you! |
b9b7bfce773e
Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
190 |