strings.org
changeset 103 587eb2416e6c
parent 100 47a2ba7beaf8
child 113 6388eacf7502
--- a/strings.org	Thu Apr 22 13:08:06 2010 +0530
+++ b/strings.org	Mon Apr 26 10:32:04 2010 +0530
@@ -17,162 +17,197 @@
 ******* lists
 ******* writing to files
 *** Script
-    Welcome friends. 
-    
-    In the previous tutorial we have looked at data types for dealing
-    with numbers. In this tutorial we shall look at strings. We shall
-    look at how to do elementary string manipulation, and simple input
-    and output operations. 
-    
-    In Python anything enclosed within quotes is a string. Lets get 
-    started by starting ipython interpreter. We shall create some 
-    string variables by:
+	Welcome friends. 
+
+	In the previous tutorial we have looked at data types for dealing
+	with numbers. In this tutorial we shall look at strings. We shall
+	look at how to do elementary string manipulation, and simple input
+	and output operations. 
+
+    In this tuotrial we shall use concepts of writing python scripts and basics of lists that have been covered in previous session
+
+	Lets get started by opening ipython interpreter.
+	We shall create some 
+	a string by typing 
 
-    a = 'This is a string'
-    print a
-    type(a) shows it is 'str'
-    b = "This too!"
-    print b
+	a = open single quote 'This is a string' close single quote
+    print a 
+    a contains the string
+	we can check for datatype of a by using type(a) and shows it is 'str'
 
-    They could either be enclosed in single or double quotes. There is
-    also a special type of string enclosed in triple single or double
-    quotes. 
+	consider the case when string contains single quote.
+	for example I'll be back
+	to store these kind of strings, we use double quotes
+	type 
+	b = open double quote "I'll be back" close double quote
+	print b ptints the value
+
+	IN python, anything enlosed in quotes is a string. Does not matter 
+	if they are single quotes or double quotes.
+
+	There is
+	also a special type of string enclosed in triple single quotes or triple double
+	quotes. 
 
-    c = '''This one too!'''
-    print c
-    d = """And one more."""
-    print d
+	so when you do 
+	c = '''Iam also a string'''
+	print c
+    and c is also string variable
+	and even 
+	d = """And one more."""
+	print d 
+    d is also a string
+
+	These strings enclosed in triple quotes are special type of strings, called docstrings, and they shall 
+	be discussed in detail along with functions
 
-    These are special type of strings, called docstrings, which shall
-    be discussed along with functions. 
-    
-    Like lists and arrays, which we have already seen, string elements 
-    can also be accessed with their indexes. The indexing here, also, 
-    begins from 0. 
+	We know elements in lists and arrays can be accessed with indices. 
+	similarly string elements 
+	can also be accessed with their indexes. and here also, indexing starts from 0
+
+	so
+	print a[0] gives us 'T' which is the first character
+	print a[5] gives us 'i' which is 6th character.
+
+	The len function, which we used with lists and arrays, works with
+	strings too. 
+	len(a) gives us the length of string a
 
-    print a[0] gives us 'T'
-    print a[5] gives us 'i' which is 6th character.
+	Python's strings support the + and * operations 
+	+ concatenates two strings.
+	so a + b gives us the two srtings concatenated
+	and * is used for replicating a string for given number of times.
+	so a * 4 gives us a replicated 4 times
+
+	What do you think would happen when you do a * a?
+	It's obviously an error since, it doesn't make any logical sense. 
+
+	One thing to note about strings, is that they are immutable, which means when yo do 
+	a[0] = 't'it throws an error
 
-    To access the last element, we can use -1 as the index!
-    print a[-1]
-    Similarly, we could access other elements with corresponding -ve
-    indexes. This is a very handy feature of python. 
+	Then how does one go about doing strings manipulations. Python provides
+	'methods' for doing various manipulations on strings. For example - 
+
+	a.upper() returns a string with all letters capitalized.
 
-    The len function, which we used with lists and arrays, works with
-    strings too. 
-    len(a)
+	and a.lower() returns a string with all smaller case letters.
+
+	there are many other methods available and we shall use Ipython auto suggestion feature to find out
+
+	type a. and hit tab
+	we can see there are many methods available in python for string manipulation
 
-    Python's strings support the operations + and *
-    + concatenates two strings.
-    a + b
-    and * is used for replicating a string for given number of times.
-    a * 4
-    What do you think would happen when you do a * a?
-    It's obviously an error since, it doesn't make any logical sense. 
-    
-    One thing to note about strings, is that they are immutable, that
-    is 
-    a[0] = 't'
-    throws an error
-    
-    Then how does one go about changing strings? Python provides
-    'methods' for doing various manipulations on strings. For example - 
+	lets us try startswith
+	a.startswith('Thi')
+	returns True if the string starts with the argument passed. 
+
+	similarly there's endswith
+	a.endswith('ING')
+
+	We've seen the use of split function in the previous
+	tutorials. split returns a list after splitting the string on the
+	given argument. 
+	alist = a.split()
+	will give list with four elements.
+	print alist
 
-    a.upper() returns a string with all letters capitalized.
+	Python also has a 'join' function, which does the opposite of what
+	split does. 
+	' '.join(alist) will return the original string a. 
+	This function takes list of elements(in our case alist) to be joined.
+	'-'.join(alist) will return a string with the spaces in the string
+	'a' replaced with hyphens. 
 
-    and a.lower() returns a string with all smaller case letters.
+	please note that after all these operations, the original string is not changed.
+	and print a prints the original string
 
-    a.startswith('Thi')
-    returns True if the string starts with the argument passed. 
+	At times we want our output or message in a particular
+	format with variables embedded, something like printf in C. For 
+	those situations python provides a provision. First lets create some 
+	variables say
+
+	In []: x, y = 1, 1.234
 
-    similarly there's endswith
-    a.endswith('ING')
+	In []: print 'x is %s, y is %s' %(x, y)
+	Out[]: 'x is 1, y is 1.234'
+	Here %s means string, you can also try %d or %f for integer and 
+	float values respectively.
+	* formatting - printf style *
+
+	we have seen how to output data
+	Now we shall look at taking input from the console.
 
-    We've seen the use of split function in the previous
-    tutorials. split returns a list after splitting the string on the
-    given argument. 
-    alist = a.split()
-    will give list with four elements.
-    print alist
+	The raw_input function allows us to take input from the console. 
+	type a = raw_input() and hit enter
+	now python is waiting for input
+	type 5 and hit enter
+
+	we can check for the value of a by typing print a and we see that it is 5
+
+	raw_input also allows us to give a prompt string.
+	we type 
+	a = raw_input("Enter a value: ")
+	and we see that the string given as argument is prompted at the user.
+	5
+	Note that a, is now a string variable and not an integer. 
+	type(a)
+	raw_input takes input only as a string
+
+	we cannot do mathematical operations on it
+    but we can use type conversion similar to that shown in previous tutorial
 
-    Python also has a 'join' function, which does the opposite of what
-    split does. 
-    ' '.join(alist) will return the original string a. 
-    This function takes list of elements(in our case alist) to be joined.
-    '-'.join(alist) will return a string with the spaces in the string
-    'a' replaced with hyphens. 
-    
-    At times we want our output or message in a particular
-    format with variables embedded, something like printf in C. For 
-    those situations python provides a provision. First lets create some 
-    variables say
-    * formatting - printf style *
-      In []: x, y = 1, 1.234
-      
-      In []: print 'x is %s, y is %s' %(x, y)
-      Out[]: 'x is 1, y is 1.234'
-      Here %s means string, you can also try %d or %f for integer and 
-      float values respectively.
-    * formatting - printf style *
+	b = int(a)
+	a has now been converted to an integer and stored in b
+	type(b) gives int
+    b can be used here for mathematical operations.
+
+	For console output, we have been using print which is pretty straightforward. 
+
+	We shall look at a subtle feature of the print statement. 
+
+	Open scite editor and type
+	print "Hello"
+	print "World"
+	We save the file as hello1.py run it from the ipython interpreter. Make
+	sure you navigate to the place, where you have saved it. 
+	%run hello1.py
+
+	Now we make a small change to the code snippet and save it in the
+	file named "hello2.py"
+	print "Hello", 
+	print "World"
+	We now run this file, from the ipython interpreter. 
+	%run hello2.py
 
 
-    Now we shall look at simple input from and output to the
-    console. 
-    The raw_input function allows us to give input from the console. 
-    a = raw_input()
-    it is now waiting for the user input. 
-    5
-    a
-    raw_input also allows us to give a prompt string, as shown 
-    a = raw_input("Enter a value: ")
-    Enter a value: 5
-    Note that a, is now a string variable and not an integer. 
-    type(a)
-    we could use type conversion similar to that shown in the tutorial
-    on numeric datatypes. 
-    a = int(a)
-    a has now been converted to an integer. 
-    type(a)
+	Note the difference in the output.
+    The comma adds a space at the end of the line, instead
+	of a new line character that is normally added. 
 
-    For console output, we use print which is pretty straightforward. 
-    We shall look at a subtle feature of the print statement. 
-    We shall first put the following code snippet in the file
-    "hello1.py"
-    print "Hello"
-    print "World"
-    We save the file and run it from the ipython interpreter. Make
-    sure you navigate to the place, where you have saved it. 
-    %run -i hello1.py
+	Before we wind up, a couple of miscellaneous things. 
+	As you may have already noticed, Python is a dynamically typed
+	language, that is you don't have to specify the type of a variable
+	when using a new one. You don't have to do anything special, to 'reuse'
+	a variable that was of int type as a float or string. 
 
-    Now we make a small change to the code snippet and save it in the
-    file named "hello2.py"
-    print "Hello", 
-    print "World"
-    We now run this file, from the ipython interpreter. 
-    %run -i hello2.py
-    Note the difference in the output of the two files that we
-    executed. The comma adds a space at the end of the line, instead
-    of a new line character that is normally added. 
+	a = 1 and here a is integer
+	lets store a float value in a by doing 
+    a = 1.1
+    and print a
+    now a is float
+	a = "Now I am a string!"
 
-    Before we wind up, a couple of miscellaneous things. 
-    As you may have already noticed, Python is a dynamically typed
-    language, that is you don't have to specify the type of a variable
-    when using a new one. You don't have to do anything special, to 'reuse'
-    a variable that was of int type as a float or string. 
-    
-    a = 1 here a is integer
-    a = 1.1 now a float
-    a = "Now I am a string!"
+	Comments in Python start with a pound or hash sign. Anything after
+	a #, until the end of the line is considered a comment, except of
+	course, if the hash is in a string. 
+	a = 1 # in-line comments
 
-    Comments in Python start with a pound or hash sign. Anything after
-    a #, until the end of the line is considered a comment, except of
-    course, if the hash is in a string. 
-    a = 1 # in-line comments
-    # a comment line
-    a = "# not a comment"
+	pritn a and we see that comment is not a part of variable a
+
+	a = "# not a comment"
 
-    we come to the end of this tutorial on strings introduction of Data types in
-    Python. In this tutorial we have learnt what are supported operations and 
-    performing simple IO operations in Python.
+	we come to the end of this tutorial on strings 
+        In this tutorial we have learnt what are supported operations on strings 
+	and how to perform simple Input and Output operations in Python.
 
 *** Notes