strings.org
changeset 100 47a2ba7beaf8
parent 99 0bc1c9ec4fcf
child 103 587eb2416e6c
equal deleted inserted replaced
99:0bc1c9ec4fcf 100:47a2ba7beaf8
    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     As, we have seen in previous tutorials, anything enclosed within
    27     In Python anything enclosed within quotes is a string. Lets get 
    28     quotes is a string. For example -
    28     started by starting ipython interpreter. We shall create some 
       
    29     string variables by:
    29 
    30 
    30     a = 'This is a string'
    31     a = 'This is a string'
    31     print a
    32     print a
       
    33     type(a) shows it is 'str'
    32     b = "This too!"
    34     b = "This too!"
    33     print b
    35     print b
    34 
    36 
    35     They could either be enclosed in single or double quotes. There is
    37     They could either be enclosed in single or double quotes. There is
    36     also a special type of string enclosed in triple single or double
    38     also a special type of string enclosed in triple single or double
    42     print d
    44     print d
    43 
    45 
    44     These are special type of strings, called docstrings, which shall
    46     These are special type of strings, called docstrings, which shall
    45     be discussed along with functions. 
    47     be discussed along with functions. 
    46     
    48     
    47     Like lists, which we already saw, string elements can be accessed
    49     Like lists and arrays, which we have already seen, string elements 
    48     with their indexes. The indexing here, also, begins from 0. 
    50     can also be accessed with their indexes. The indexing here, also, 
       
    51     begins from 0. 
    49 
    52 
    50     print a[0]    
    53     print a[0] gives us 'T'
    51     print a[5]
    54     print a[5] gives us 'i' which is 6th character.
    52 
    55 
    53     To access the last element, we can use -1 as the index!
    56     To access the last element, we can use -1 as the index!
    54     print a[-1]
    57     print a[-1]
    55     Similarly, we could access other elements with corresponding -ve
    58     Similarly, we could access other elements with corresponding -ve
    56     indexes. This is a very handy feature of python. 
    59     indexes. This is a very handy feature of python. 
    58     The len function, which we used with lists and arrays, works with
    61     The len function, which we used with lists and arrays, works with
    59     strings too. 
    62     strings too. 
    60     len(a)
    63     len(a)
    61 
    64 
    62     Python's strings support the operations + and *
    65     Python's strings support the operations + and *
       
    66     + concatenates two strings.
    63     a + b
    67     a + b
       
    68     and * is used for replicating a string for given number of times.
    64     a * 4
    69     a * 4
    65     What do you think would happen when you do a * a?
    70     What do you think would happen when you do a * a?
    66     It's obviously an error since, it doesn't make any logical sense. 
    71     It's obviously an error since, it doesn't make any logical sense. 
    67     
    72     
    68     One thing to note about strings, is that they are immutable, that
    73     One thing to note about strings, is that they are immutable, that
    91     print alist
    96     print alist
    92 
    97 
    93     Python also has a 'join' function, which does the opposite of what
    98     Python also has a 'join' function, which does the opposite of what
    94     split does. 
    99     split does. 
    95     ' '.join(alist) will return the original string a. 
   100     ' '.join(alist) will return the original string a. 
       
   101     This function takes list of elements(in our case alist) to be joined.
    96     '-'.join(alist) will return a string with the spaces in the string
   102     '-'.join(alist) will return a string with the spaces in the string
    97     'a' replaced with hyphens. 
   103     'a' replaced with hyphens. 
    98     
   104     
    99     At times we want our output or message in a particular
   105     At times we want our output or message in a particular
   100     format with variables embedded, something like printf in C. For 
   106     format with variables embedded, something like printf in C. For 
   101     those situations python provides a provision. First lets create some 
   107     those situations python provides a provision. First lets create some 
   102     variables
   108     variables say
   103     * formatting - printf style *
   109     * formatting - printf style *
   104       In []: x, y = 1, 1.234
   110       In []: x, y = 1, 1.234
   105       
   111       
   106       In []: print 'x is %s, y is %s' %(x, y)
   112       In []: print 'x is %s, y is %s' %(x, y)
   107       Out[]: 'x is 1, y is 1.234'
   113       Out[]: 'x is 1, y is 1.234'
   108       Here %s means string, you can also try %d or %f for integer and 
   114       Here %s means string, you can also try %d or %f for integer and 
   109       float values.
   115       float values respectively.
   110     * formatting - printf style *
   116     * formatting - printf style *
   111 
   117 
   112 
   118 
   113     Now we shall look at simple input from and output to the
   119     Now we shall look at simple input from and output to the
   114     console. 
   120     console. 
   149     of a new line character that is normally added. 
   155     of a new line character that is normally added. 
   150 
   156 
   151     Before we wind up, a couple of miscellaneous things. 
   157     Before we wind up, a couple of miscellaneous things. 
   152     As you may have already noticed, Python is a dynamically typed
   158     As you may have already noticed, Python is a dynamically typed
   153     language, that is you don't have to specify the type of a variable
   159     language, that is you don't have to specify the type of a variable
   154     when using a new one. You don't have to do anything special, to use
   160     when using a new one. You don't have to do anything special, to 'reuse'
   155     a variable that was of int type as a float or string. 
   161     a variable that was of int type as a float or string. 
   156     
   162     
   157     a = 1
   163     a = 1 here a is integer
   158     a = 1.1
   164     a = 1.1 now a float
   159     a = "Now I am a string!"
   165     a = "Now I am a string!"
   160 
   166 
   161     Comments in Python start with a pound or hash sign. Anything after
   167     Comments in Python start with a pound or hash sign. Anything after
   162     a #, until the end of the line is considered a comment, except of
   168     a #, until the end of the line is considered a comment, except of
   163     course, if the hash is in a string. 
   169     course, if the hash is in a string. 
   164     a = 1 # in-line comments
   170     a = 1 # in-line comments
   165     # a comment line
   171     # a comment line
   166     a = "# not a comment"
   172     a = "# not a comment"
   167 
   173 
   168     we come to the end of this tutorial on strings introduction of Data types in
   174     we come to the end of this tutorial on strings introduction of Data types in
   169     Python. In this tutorial we have learnt what are supported data types, 
   175     Python. In this tutorial we have learnt what are supported operations and 
   170     supported operations and performing simple IO operations in Python.
   176     performing simple IO operations in Python.
   171 
   177 
   172 *** Notes
   178 *** Notes