strings.org
changeset 113 6388eacf7502
parent 103 587eb2416e6c
equal deleted inserted replaced
112:7de6be45182e 113:6388eacf7502
    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 this tuotrial we shall use concepts of writing python scripts and basics of lists that have been covered in previous session
    27 	In this tuotrial we shall use concepts of writing python scripts and 
       
    28 	basics of lists that have been covered in previous session
    28 
    29 
    29 	Lets get started by opening ipython interpreter.
    30 	Lets get started by opening ipython interpreter.
    30 	We shall create some 
    31 	We shall create some 
    31 	a string by typing 
    32 	a string by typing 
    32 
    33 
    33 	a = open single quote 'This is a string' close single quote
    34 	a = open single quote 'This is a string' close single quote
    34     print a 
    35 	print a 
    35     a contains the string
    36 	a contains the string
    36 	we can check for datatype of a by using type(a) and shows it is 'str'
    37 	we can check for datatype of a by using type(a) and shows it is 'str'
    37 
    38 
    38 	consider the case when string contains single quote.
    39 	consider the case when string contains single quote.
    39 	for example I'll be back
    40 	for example I'll be back
    40 	to store these kind of strings, we use double quotes
    41 	to store these kind of strings, we use double quotes
    50 	quotes. 
    51 	quotes. 
    51 
    52 
    52 	so when you do 
    53 	so when you do 
    53 	c = '''Iam also a string'''
    54 	c = '''Iam also a string'''
    54 	print c
    55 	print c
    55     and c is also string variable
    56 	and c is also string variable
    56 	and even 
    57 	and even 
    57 	d = """And one more."""
    58 	d = """And one more."""
    58 	print d 
    59 	print d 
    59     d is also a string
    60 	d is also a string
    60 
    61 
    61 	These strings enclosed in triple quotes are special type of strings, called docstrings, and they shall 
    62 	These strings enclosed in triple quotes are special type of strings, called docstrings, and they shall 
    62 	be discussed in detail along with functions
    63 	be discussed in detail along with functions
    63 
    64 
    64 	We know elements in lists and arrays can be accessed with indices. 
    65 	We know elements in lists and arrays can be accessed with indices. 
   152 	Note that a, is now a string variable and not an integer. 
   153 	Note that a, is now a string variable and not an integer. 
   153 	type(a)
   154 	type(a)
   154 	raw_input takes input only as a string
   155 	raw_input takes input only as a string
   155 
   156 
   156 	we cannot do mathematical operations on it
   157 	we cannot do mathematical operations on it
   157     but we can use type conversion similar to that shown in previous tutorial
   158 	but we can use type conversion similar to that shown in previous tutorial
   158 
   159 
   159 	b = int(a)
   160 	b = int(a)
   160 	a has now been converted to an integer and stored in b
   161 	a has now been converted to an integer and stored in b
   161 	type(b) gives int
   162 	type(b) gives int
   162     b can be used here for mathematical operations.
   163 	b can be used here for mathematical operations.
   163 
   164 
   164 	For console output, we have been using print which is pretty straightforward. 
   165 	For console output, we have been using print which is pretty straightforward. 
   165 
   166 
   166 	We shall look at a subtle feature of the print statement. 
   167 	We shall look at a subtle feature of the print statement. 
   167 
   168 
   179 	We now run this file, from the ipython interpreter. 
   180 	We now run this file, from the ipython interpreter. 
   180 	%run hello2.py
   181 	%run hello2.py
   181 
   182 
   182 
   183 
   183 	Note the difference in the output.
   184 	Note the difference in the output.
   184     The comma adds a space at the end of the line, instead
   185 	The comma adds a space at the end of the line, instead
   185 	of a new line character that is normally added. 
   186 	of a new line character that is normally added. 
   186 
   187 
   187 	Before we wind up, a couple of miscellaneous things. 
   188 	Before we wind up, a couple of miscellaneous things. 
   188 	As you may have already noticed, Python is a dynamically typed
   189 	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 	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 	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 	a variable that was of int type as a float or string. 
   192 
   193 
   193 	a = 1 and here a is integer
   194 	a = 1 and here a is integer
   194 	lets store a float value in a by doing 
   195 	lets store a float value in a by doing 
   195     a = 1.1
   196 	a = 1.1
   196     and print a
   197 	and print a
   197     now a is float
   198 	now a is float
   198 	a = "Now I am a string!"
   199 	a = "Now I am a string!"
   199 
   200 
   200 	Comments in Python start with a pound or hash sign. Anything after
   201 	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 	a #, until the end of the line is considered a comment, except of
   202 	course, if the hash is in a string. 
   203 	course, if the hash is in a string.