|
1 .. Objectives |
|
2 .. ---------- |
|
3 |
|
4 .. At the end of this tutorial, you should know -- |
|
5 |
|
6 .. 1. How to define strings |
|
7 .. #. Different ways of defining a string |
|
8 .. #. How to concatenate strings |
|
9 .. #. How to print a string repeatedly |
|
10 .. #. Accessing individual elements of the string |
|
11 .. #. Immutability of strings |
|
12 |
|
13 .. Prerequisites |
|
14 .. ------------- |
|
15 |
|
16 .. 1. getting started with ipython |
|
17 |
|
18 .. Author : Madhu |
|
19 Internal Reviewer : |
|
20 External Reviewer : |
|
21 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
|
22 |
|
23 Script |
|
24 ------ |
|
25 |
|
26 {{{ Show the slide containing the title }}} |
|
27 |
|
28 Hello friends. Welcome to this spoken tutorial on Getting started with |
|
29 strings. |
|
30 |
|
31 {{{ Show the slide containing the outline }}} |
|
32 |
|
33 In this tutorial, we will learn what do we actually mean by strings in |
|
34 python, how python supports the use of strings. We will also learn |
|
35 some of the operations that can be performed on strings. |
|
36 |
|
37 {{{ Shift to terminal and start ipython }}} |
|
38 |
|
39 To begin with let us start ipython, by typing:: |
|
40 |
|
41 ipython |
|
42 |
|
43 on the terminal |
|
44 |
|
45 So what are strings? In Python anything within either single quotes |
|
46 or double quotes or triple single quotes or triple double quotes are |
|
47 strings. This is true whatsoever, even if there is only one character |
|
48 within the quotes |
|
49 |
|
50 {{{ Type in ipython the following and read them as you type }}}:: |
|
51 |
|
52 'This is a string' |
|
53 "This is a string too' |
|
54 '''This is a string as well''' |
|
55 """This is also a string""" |
|
56 'p' |
|
57 |
|
58 Having more than one control character to define strings come as very |
|
59 handy when one of the control characters itself is part of the |
|
60 string. For example:: |
|
61 |
|
62 "Python's string manipulation functions are very useful" |
|
63 |
|
64 In this case we use single quote for apostrophe. If we had only single |
|
65 quote to define strings we should have a clumsy way of escaping the |
|
66 single quote character to make it part of the string. Hence this is a |
|
67 very handy feature. |
|
68 |
|
69 The triple quoted strings let us define multi-lines strings without |
|
70 using any escaping. Everything within the triple quotes is a single |
|
71 string no matter how many lines it extends:: |
|
72 |
|
73 """Having more than one control character to define |
|
74 strings come as very handy when one of the control |
|
75 characters itself is part of the string.""" |
|
76 |
|
77 We can assign this string to any variable:: |
|
78 |
|
79 a = 'Hello, World!' |
|
80 |
|
81 Now 'a' is a string variable. String is a collection of characters. In |
|
82 addition string is an immutable collection. So all the operations that |
|
83 are applicable to any other immutable collection in Python works on |
|
84 string as well. So we can add two strings:: |
|
85 |
|
86 a = 'Hello' |
|
87 b = 'World' |
|
88 c = a + ', ' + b + '!' |
|
89 |
|
90 We can add string variables as well as the strings themselves all in |
|
91 the same statement. The addition operation performs the concatenation |
|
92 of two strings. |
|
93 |
|
94 Similarly we can multiply a string with an integer:: |
|
95 |
|
96 a = 'Hello' |
|
97 a * 5 |
|
98 |
|
99 gives another string in which the original string 'Hello' is repeated |
|
100 5 times. |
|
101 |
|
102 Since strings are collections we can access individual items in the |
|
103 string using the subscripts:: |
|
104 |
|
105 a[0] |
|
106 |
|
107 gives us the first character in the string. The indexing starts from 0 |
|
108 for the first character up to n-1 for the last character. We can |
|
109 access the strings from the end using negative indices:: |
|
110 |
|
111 a[-2] |
|
112 |
|
113 gives us second element from the end of the string |
|
114 |
|
115 Let us attempt to change one of the characters in a string:: |
|
116 |
|
117 a = 'hello' |
|
118 a[0] = 'H' |
|
119 |
|
120 As said earlier, strings are immutable. We cannot manipulate the |
|
121 string. Although there are some methods which let us to manipulate the |
|
122 strings. We will look at them in the advanced session on strings. In |
|
123 addition to the methods that let us manipulate the strings we have |
|
124 methods like split which lets us break the string on the specified |
|
125 separator, the join method which lets us combine the list of strings |
|
126 into a single string based on the specified separator. |
|
127 |
|
128 {{{ Show summary slide }}} |
|
129 |
|
130 This brings us to the end of another session. In this tutorial session |
|
131 we learnt |
|
132 |
|
133 * How to define strings |
|
134 * Different ways of defining a string |
|
135 * String concatenation and repeatition |
|
136 * Accessing individual elements of the string |
|
137 * Immutability of strings |
|
138 |
|
139 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
140 |
|
141 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
142 |
|
143 Hope you have enjoyed and found it useful. |
|
144 Thank you! |
|
145 |