29 assignment. |
29 assignment. |
30 |
30 |
31 String Formatting |
31 String Formatting |
32 ================= |
32 ================= |
33 |
33 |
|
34 String formatting can be performed using the string formatting operator represented |
|
35 as the percent (%) sign. The string placed before the % sign is formatted with |
|
36 the value placed to the right of it. Let us look at a simple example. |
|
37 |
|
38 :: |
|
39 |
|
40 >>> format = 'Hello %s, from PythonFreak' |
|
41 >>> str1 = 'world!' |
|
42 >>> print format % str1 |
|
43 Hello world!, from PythonFreak |
|
44 |
|
45 The %s parts of the format string are called the coversion specifiers. The coversion |
|
46 specifiers mark the places where the formatting has to be performed in a string. |
|
47 In the example the %s is replaced by the value of str1. More than one value can |
|
48 also be formatted at a time by specifying the values to be formatted using tuples |
|
49 and dictionaries (explained in later sections). Let us look at an example. |
|
50 |
|
51 :: |
|
52 |
|
53 >>> format = 'Hello %s, from %s' |
|
54 >>> values = ('world!', 'PythonFreak') |
|
55 >>> print format % values |
|
56 Hello world!, from PythonFreak |
|
57 |
|
58 In this example it can be observed that the format string contains two conversion |
|
59 specifiers and they are formatted using the tuple of values as shown. |
|
60 |
|
61 The s in %s specifies that the value to be replaced is of type string. Values of |
|
62 other types can be specified as well such as integers and floats. Integers are |
|
63 specified as %d and floats as %f. The precision with which the integer or the |
|
64 float values are to be represented can also be specified using a **.** (**dot**) |
|
65 followed by the precision value. |
|
66 |
|
67 String Methods |
|
68 ============== |
|
69 |
|
70 Similar to list methods, strings also have a rich set of methods to perform various |
|
71 operations on strings. Some of the most important and popular ones are presented |
|
72 in this section. |
|
73 |
|
74 **find** |
|
75 ~~~~~~~~ |
|
76 |
|
77 The **find** method is used to search for a substring within a given string. It |
|
78 returns the left most index of the first occurence of the substring. If the |
|
79 substring is not found in the string then it returns -1. Let us look at a few |
|
80 examples. |
|
81 |
|
82 :: |
|
83 |
|
84 >>> longstring = 'Hello world!, from PythonFreak' |
|
85 >>> longstring.find('Python') |
|
86 19 |
|
87 >>> longstring.find('Perl') |
|
88 -1 |
|
89 |
|
90 **join** |
|
91 ~~~~~~~~ |
|
92 |
|
93 The **join** method is used to join the elements of a sequence. The sequence |
|
94 elements that are to be join ed should all be strings. Let us look at a few |
|
95 examples. |
|
96 |
|
97 :: |
|
98 |
|
99 >>> seq = ['With', 'great', 'power', 'comes', 'great', 'responsibility'] |
|
100 >>> sep = ' ' |
|
101 >>> sep.join(seq) |
|
102 'With great power comes great responsibility' |
|
103 >>> sep = ',!' |
|
104 >>> sep.join(seq) |
|
105 'With,!great,!power,!comes,!great,!responsibility' |
|
106 |
|
107 *Try this yourself* |
|
108 |
|
109 :: |
|
110 |
|
111 >>> seq = [12,34,56,78] |
|
112 >>> sep.join(seq) |
|
113 |
|
114 **lower** |
|
115 ~~~~~~~~~ |
|
116 |
|
117 The **lower** method, as the name indicates, converts the entire text of a string |
|
118 to lower case. It is specially useful in cases where the programmers deal with case |
|
119 insensitive data. Let us look at a few examples. |
|
120 |
|
121 :: |
|
122 |
|
123 >>> sometext = 'Hello world!, from PythonFreak' |
|
124 >>> sometext.lower() |
|
125 'hello world!, from pythonfreak' |
|
126 |
|
127 **replace** |
|
128 ~~~~~~~~~~~ |
|
129 |
|
130 The **replace** method replaces a substring with another substring within |
|
131 a given string and returns the new string. Let us look at an example. |
|
132 |
|
133 :: |
|
134 |
|
135 >>> sometext = 'Concise, precise and criticise is some of the words that end with ise' |
|
136 >>> sometext.replace('is', 'are') |
|
137 'Concaree, precaree and criticaree are some of the words that end with aree' |
|
138 |
|
139 Observe here that all the occurences of the substring *is* have been replaced, |
|
140 even the *is* in *concise*, *precise* and *criticise* have been replaced. |
|
141 |
|
142 **split** |
|
143 ~~~~~~~~~ |
|
144 |
|
145 The **split** is one of the very important string methods. split is the opposite of the |
|
146 **join** method. It is used to split a string based on the argument passed as the |
|
147 delimiter. It returns a list of strings. By default when no argument is passed it |
|
148 splits with *space* (' ') as the delimiter. Let us look at an example. |
|
149 |
|
150 :: |
|
151 |
|
152 >>> grocerylist = 'butter, cucumber, beer(a grocery item??), wheatbread' |
|
153 >>> grocerylist.split(',') |
|
154 ['butter', ' cucumber', ' beer(a grocery item??)', ' wheatbread'] |
|
155 >>> grocerylist.split() |
|
156 ['butter,', 'cucumber,', 'beer(a', 'grocery', 'item??),', 'wheatbread'] |
|
157 |
|
158 Observe here that in the second case when the delimiter argument was not set |
|
159 **split** was done with *space* as the delimiter. |
|
160 |
|
161 **strip** |
|
162 ~~~~~~~~~ |
|
163 |
|
164 The **strip** method is used to remove or **strip** off any whitespaces that exist |
|
165 to the left and right of a string, but not the whitespaces within a string. Let |
|
166 us look at an example. |
|
167 |
|
168 :: |
|
169 |
|
170 >>> spacedtext = " Where's the text?? " |
|
171 >>> spacedtext.strip() |
|
172 "Where's the text??" |
|
173 |
|
174 Observe that the whitespaces between the words have not been removed. |
|
175 |
|
176 :: |
|
177 |
|
178 Note: Very important thing to note is that all the methods shown above do not |
|
179 transform the source string. The source string still remains the same. |
|
180 Remember that **strings are immutable**. |
|
181 |
|
182 |
|
183 I/O: Reading and Writing Files |
|
184 ============================== |
|
185 |
|
186 Files are very important aspects when it comes to computing and programming. |
|
187 Up until now the focus has been on small programs that interacted with users |
|
188 through **input()** and **raw_input()**. Generally, for computational purposes |
|
189 it becomes necessary to handle files, which are usually large in size as well. |
|
190 This section focuses on basics of file handling. |
|
191 |
|
192 Opening Files |
|
193 ~~~~~~~~~~~~~ |
|
194 |
|
195 Files can be opened using the **open()** method. **open()** accepts 3 arguments |
|
196 out of which 2 are optional. Let us look at the syntax of **open()**: |
|
197 |
|
198 *f = open( filename, mode, buffering)* |
|
199 |
|
200 The *filename* is a compulsory argument while the *mode* and *buffering* are |
|
201 optional. The *filename* should be a string and it should be the complete path |
|
202 to the file to be opened (The path can be absolute or relative). Let us look at |
|
203 an example. |
|
204 |
|
205 :: |
|
206 |
|
207 >>> f = open ('basic_python/interim_assessment.rst') |
|
208 |
|
209 The *mode* argument specifies the mode in which the file has to be opened. |
|
210 The following are the valid mode arguments: |
|
211 |
|
212 **r** - Read mode |
|
213 **w** - Write mode |
|
214 **a** - Append mode |
|
215 **b** - Binary mode |
|
216 **+** - Read/Write mode |
|
217 |
|
218 The read mode opens the file as a read-only document. The write mode opens the |
|
219 file in the Write only mode. In the write mode, if the file existed prior to the |
|
220 opening, the previous contents of the file are erased. The append mode opens the |
|
221 file in the write mode but the previous contents of the file are not erased and |
|
222 the current data is appended onto the file. |
|
223 The binary and the read/write modes are special in the sense that they are added |
|
224 onto other modes. The read/write mode opens the file in the reading and writing |
|
225 mode combined. The binary mode can be used to open a files that do not contain |
|
226 text. Binary files such as images should be opened in the binary mode. Let us look |
|
227 at a few examples. |
|
228 |
|
229 :: |
|
230 |
|
231 >>> f = open ('basic_python/interim_assessment.rst', 'r') |
|
232 >>> f = open ('armstrong.py', 'r+') |
|
233 |
|
234 The third argument to the **open()** method is the *buffering* argument. This takes |
|
235 a boolean value, *True* or *1* indicates that buffering has to be enabled on the file, |
|
236 that is the file is loaded on to the main memory and the changes made to the file are |
|
237 not immediately written to the disk. If the *buffering* argument is *0* or *False* the |
|
238 changes are directly written on to the disk immediately. |
|
239 |
|
240 Reading and Writing files |
|
241 ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
242 |
|
243 **write()** |
|
244 ----------- |
|
245 |
|
246 **write()**, evidently, is used to write data onto a file. It takes the data to |
|
247 be written as the argument. The data can be a string, an integer, a float or any |
|
248 other datatype. In order to be able to write data onto a file, the file has to |
|
249 be opened in one of **w**, **a** or **+** modes. |
|
250 |
|
251 **read()** |
|
252 ---------- |
|
253 |
|
254 **read()** is used to read data from a file. It takes the number of bytes of data |
|
255 to be read as the argument. If nothing is specified by default it reads the entire |
|
256 contents from the current position to the end of file. |
|
257 |
|
258 Let us look at a few examples: |
|
259 |
|
260 :: |
|
261 |
|
262 >>> f = open ('randomtextfile', 'w') |
|
263 >>> f.write('Hello all, this is PythonFreak. This is a random text file.') |
|
264 >>> f = open ('../randomtextfile', 'r') |
|
265 >>> f = open ('../randomtextfile', 'r') |
|
266 >>> f.read(5) |
|
267 'Hello' |
|
268 >>> f.read() |
|
269 ' all, this is PythonFreak. This is a random text file.' |
|
270 >>> f.close() |
|
271 |
|
272 **readline()** |
|
273 -------------- |
|
274 |
|
275 **readline()** is used to read a file line by line. **readline()** reads a line |
|
276 of a file at a time. When an argument is passed to **readline()** it reads that |
|
277 many bytes from the current line. |
|
278 |
|
279 One other method to read a file line by line is using the **read()** and the |
|
280 **for** construct. Let us look at this block of code as an example. |
|
281 |
|
282 :: |
|
283 |
|
284 >>> f = open('../randomtextfile', 'r') |
|
285 >>> for line in f: |
|
286 ... print line |
|
287 ... |
|
288 Hello all! |
|
289 |
|
290 This is PythonFreak on the second line. |
|
291 |
|
292 This is a random text file on line 3 |
|
293 |
|
294 **close()** |
|
295 ----------- |
|
296 |
|
297 One must always close all the files that have been opened. Although, files opened |
|
298 will be closed automatically when the program ends. When files opened in read mode |
|
299 are not closed it might lead to uselessly locked sometimes. In case of files |
|
300 opened in the write mode it is more important to close the files. This is because, |
|
301 Python maybe using the file in the buffering mode and when the file is not closed |
|
302 the buffer maybe lost completely and the changes made to the file are lost forever. |
|
303 |
|
304 |
|
305 Dictionaries |
|
306 ============ |
|
307 |