15 ******* comments |
15 ******* comments |
16 ***** Arsenal Required |
16 ***** Arsenal Required |
17 ******* lists |
17 ******* lists |
18 ******* writing to files |
18 ******* writing to files |
19 *** Script |
19 *** Script |
20 Welcome friends. |
20 Welcome friends. |
21 |
21 |
22 In the previous tutorial we have looked at data types for dealing |
22 In the previous tutorial we have looked at data types for dealing |
23 with numbers. In this tutorial we shall look at strings. We shall |
23 with numbers. In this tutorial we shall look at strings. We shall |
24 look at how to do elementary string manipulation, and simple input |
24 look at how to do elementary string manipulation, and simple input |
25 and output operations. |
25 and output operations. |
26 |
26 |
27 In Python anything enclosed within quotes is a string. Lets get |
27 In this tuotrial we shall use concepts of writing python scripts and basics of lists that have been covered in previous session |
28 started by starting ipython interpreter. We shall create some |
28 |
29 string variables by: |
29 Lets get started by opening ipython interpreter. |
30 |
30 We shall create some |
31 a = 'This is a string' |
31 a string by typing |
32 print a |
32 |
33 type(a) shows it is 'str' |
33 a = open single quote 'This is a string' close single quote |
34 b = "This too!" |
34 print a |
35 print b |
35 a contains the string |
36 |
36 we can check for datatype of a by using type(a) and shows it is 'str' |
37 They could either be enclosed in single or double quotes. There is |
37 |
38 also a special type of string enclosed in triple single or double |
38 consider the case when string contains single quote. |
39 quotes. |
39 for example I'll be back |
40 |
40 to store these kind of strings, we use double quotes |
41 c = '''This one too!''' |
41 type |
42 print c |
42 b = open double quote "I'll be back" close double quote |
43 d = """And one more.""" |
43 print b ptints the value |
44 print d |
44 |
45 |
45 IN python, anything enlosed in quotes is a string. Does not matter |
46 These are special type of strings, called docstrings, which shall |
46 if they are single quotes or double quotes. |
47 be discussed along with functions. |
47 |
48 |
48 There is |
49 Like lists and arrays, which we have already seen, string elements |
49 also a special type of string enclosed in triple single quotes or triple double |
50 can also be accessed with their indexes. The indexing here, also, |
50 quotes. |
51 begins from 0. |
51 |
52 |
52 so when you do |
53 print a[0] gives us 'T' |
53 c = '''Iam also a string''' |
54 print a[5] gives us 'i' which is 6th character. |
54 print c |
55 |
55 and c is also string variable |
56 To access the last element, we can use -1 as the index! |
56 and even |
57 print a[-1] |
57 d = """And one more.""" |
58 Similarly, we could access other elements with corresponding -ve |
58 print d |
59 indexes. This is a very handy feature of python. |
59 d is also a string |
60 |
60 |
61 The len function, which we used with lists and arrays, works with |
61 These strings enclosed in triple quotes are special type of strings, called docstrings, and they shall |
62 strings too. |
62 be discussed in detail along with functions |
63 len(a) |
63 |
64 |
64 We know elements in lists and arrays can be accessed with indices. |
65 Python's strings support the operations + and * |
65 similarly string elements |
66 + concatenates two strings. |
66 can also be accessed with their indexes. and here also, indexing starts from 0 |
67 a + b |
67 |
68 and * is used for replicating a string for given number of times. |
68 so |
69 a * 4 |
69 print a[0] gives us 'T' which is the first character |
70 What do you think would happen when you do a * a? |
70 print a[5] gives us 'i' which is 6th character. |
71 It's obviously an error since, it doesn't make any logical sense. |
71 |
72 |
72 The len function, which we used with lists and arrays, works with |
73 One thing to note about strings, is that they are immutable, that |
73 strings too. |
74 is |
74 len(a) gives us the length of string a |
75 a[0] = 't' |
75 |
76 throws an error |
76 Python's strings support the + and * operations |
77 |
77 + concatenates two strings. |
78 Then how does one go about changing strings? Python provides |
78 so a + b gives us the two srtings concatenated |
79 'methods' for doing various manipulations on strings. For example - |
79 and * is used for replicating a string for given number of times. |
80 |
80 so a * 4 gives us a replicated 4 times |
81 a.upper() returns a string with all letters capitalized. |
81 |
82 |
82 What do you think would happen when you do a * a? |
83 and a.lower() returns a string with all smaller case letters. |
83 It's obviously an error since, it doesn't make any logical sense. |
84 |
84 |
85 a.startswith('Thi') |
85 One thing to note about strings, is that they are immutable, which means when yo do |
86 returns True if the string starts with the argument passed. |
86 a[0] = 't'it throws an error |
87 |
87 |
88 similarly there's endswith |
88 Then how does one go about doing strings manipulations. Python provides |
89 a.endswith('ING') |
89 'methods' for doing various manipulations on strings. For example - |
90 |
90 |
91 We've seen the use of split function in the previous |
91 a.upper() returns a string with all letters capitalized. |
92 tutorials. split returns a list after splitting the string on the |
92 |
93 given argument. |
93 and a.lower() returns a string with all smaller case letters. |
94 alist = a.split() |
94 |
95 will give list with four elements. |
95 there are many other methods available and we shall use Ipython auto suggestion feature to find out |
96 print alist |
96 |
97 |
97 type a. and hit tab |
98 Python also has a 'join' function, which does the opposite of what |
98 we can see there are many methods available in python for string manipulation |
99 split does. |
99 |
100 ' '.join(alist) will return the original string a. |
100 lets us try startswith |
101 This function takes list of elements(in our case alist) to be joined. |
101 a.startswith('Thi') |
102 '-'.join(alist) will return a string with the spaces in the string |
102 returns True if the string starts with the argument passed. |
103 'a' replaced with hyphens. |
103 |
104 |
104 similarly there's endswith |
105 At times we want our output or message in a particular |
105 a.endswith('ING') |
106 format with variables embedded, something like printf in C. For |
106 |
107 those situations python provides a provision. First lets create some |
107 We've seen the use of split function in the previous |
108 variables say |
108 tutorials. split returns a list after splitting the string on the |
109 * formatting - printf style * |
109 given argument. |
110 In []: x, y = 1, 1.234 |
110 alist = a.split() |
111 |
111 will give list with four elements. |
112 In []: print 'x is %s, y is %s' %(x, y) |
112 print alist |
113 Out[]: 'x is 1, y is 1.234' |
113 |
114 Here %s means string, you can also try %d or %f for integer and |
114 Python also has a 'join' function, which does the opposite of what |
115 float values respectively. |
115 split does. |
116 * formatting - printf style * |
116 ' '.join(alist) will return the original string a. |
117 |
117 This function takes list of elements(in our case alist) to be joined. |
118 |
118 '-'.join(alist) will return a string with the spaces in the string |
119 Now we shall look at simple input from and output to the |
119 'a' replaced with hyphens. |
120 console. |
120 |
121 The raw_input function allows us to give input from the console. |
121 please note that after all these operations, the original string is not changed. |
122 a = raw_input() |
122 and print a prints the original string |
123 it is now waiting for the user input. |
123 |
124 5 |
124 At times we want our output or message in a particular |
125 a |
125 format with variables embedded, something like printf in C. For |
126 raw_input also allows us to give a prompt string, as shown |
126 those situations python provides a provision. First lets create some |
127 a = raw_input("Enter a value: ") |
127 variables say |
128 Enter a value: 5 |
128 |
129 Note that a, is now a string variable and not an integer. |
129 In []: x, y = 1, 1.234 |
130 type(a) |
130 |
131 we could use type conversion similar to that shown in the tutorial |
131 In []: print 'x is %s, y is %s' %(x, y) |
132 on numeric datatypes. |
132 Out[]: 'x is 1, y is 1.234' |
133 a = int(a) |
133 Here %s means string, you can also try %d or %f for integer and |
134 a has now been converted to an integer. |
134 float values respectively. |
135 type(a) |
135 * formatting - printf style * |
136 |
136 |
137 For console output, we use print which is pretty straightforward. |
137 we have seen how to output data |
138 We shall look at a subtle feature of the print statement. |
138 Now we shall look at taking input from the console. |
139 We shall first put the following code snippet in the file |
139 |
140 "hello1.py" |
140 The raw_input function allows us to take input from the console. |
141 print "Hello" |
141 type a = raw_input() and hit enter |
142 print "World" |
142 now python is waiting for input |
143 We save the file and run it from the ipython interpreter. Make |
143 type 5 and hit enter |
144 sure you navigate to the place, where you have saved it. |
144 |
145 %run -i hello1.py |
145 we can check for the value of a by typing print a and we see that it is 5 |
146 |
146 |
147 Now we make a small change to the code snippet and save it in the |
147 raw_input also allows us to give a prompt string. |
148 file named "hello2.py" |
148 we type |
149 print "Hello", |
149 a = raw_input("Enter a value: ") |
150 print "World" |
150 and we see that the string given as argument is prompted at the user. |
151 We now run this file, from the ipython interpreter. |
151 5 |
152 %run -i hello2.py |
152 Note that a, is now a string variable and not an integer. |
153 Note the difference in the output of the two files that we |
153 type(a) |
154 executed. The comma adds a space at the end of the line, instead |
154 raw_input takes input only as a string |
155 of a new line character that is normally added. |
155 |
156 |
156 we cannot do mathematical operations on it |
157 Before we wind up, a couple of miscellaneous things. |
157 but we can use type conversion similar to that shown in previous tutorial |
158 As you may have already noticed, Python is a dynamically typed |
158 |
159 language, that is you don't have to specify the type of a variable |
159 b = int(a) |
160 when using a new one. You don't have to do anything special, to 'reuse' |
160 a has now been converted to an integer and stored in b |
161 a variable that was of int type as a float or string. |
161 type(b) gives int |
162 |
162 b can be used here for mathematical operations. |
163 a = 1 here a is integer |
163 |
164 a = 1.1 now a float |
164 For console output, we have been using print which is pretty straightforward. |
165 a = "Now I am a string!" |
165 |
166 |
166 We shall look at a subtle feature of the print statement. |
167 Comments in Python start with a pound or hash sign. Anything after |
167 |
168 a #, until the end of the line is considered a comment, except of |
168 Open scite editor and type |
169 course, if the hash is in a string. |
169 print "Hello" |
170 a = 1 # in-line comments |
170 print "World" |
171 # a comment line |
171 We save the file as hello1.py run it from the ipython interpreter. Make |
172 a = "# not a comment" |
172 sure you navigate to the place, where you have saved it. |
173 |
173 %run hello1.py |
174 we come to the end of this tutorial on strings introduction of Data types in |
174 |
175 Python. In this tutorial we have learnt what are supported operations and |
175 Now we make a small change to the code snippet and save it in the |
176 performing simple IO operations in Python. |
176 file named "hello2.py" |
|
177 print "Hello", |
|
178 print "World" |
|
179 We now run this file, from the ipython interpreter. |
|
180 %run hello2.py |
|
181 |
|
182 |
|
183 Note the difference in the output. |
|
184 The comma adds a space at the end of the line, instead |
|
185 of a new line character that is normally added. |
|
186 |
|
187 Before we wind up, a couple of miscellaneous things. |
|
188 As you may have already noticed, Python is a dynamically typed |
|
189 language, that is you don't have to specify the type of a variable |
|
190 when using a new one. You don't have to do anything special, to 'reuse' |
|
191 a variable that was of int type as a float or string. |
|
192 |
|
193 a = 1 and here a is integer |
|
194 lets store a float value in a by doing |
|
195 a = 1.1 |
|
196 and print a |
|
197 now a is float |
|
198 a = "Now I am a string!" |
|
199 |
|
200 Comments in Python start with a pound or hash sign. Anything after |
|
201 a #, until the end of the line is considered a comment, except of |
|
202 course, if the hash is in a string. |
|
203 a = 1 # in-line comments |
|
204 |
|
205 pritn a and we see that comment is not a part of variable a |
|
206 |
|
207 a = "# not a comment" |
|
208 |
|
209 we come to the end of this tutorial on strings |
|
210 In this tutorial we have learnt what are supported operations on strings |
|
211 and how to perform simple Input and Output operations in Python. |
177 |
212 |
178 *** Notes |
213 *** Notes |