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