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