strings.org
changeset 97 25248b12f6e4
parent 96 3498d74ed615
child 99 0bc1c9ec4fcf
equal deleted inserted replaced
96:3498d74ed615 97:25248b12f6e4
     5 ******* operations
     5 ******* operations
     6 ******* immutability
     6 ******* immutability
     7 ******* string methods
     7 ******* string methods
     8 ******* split and join
     8 ******* split and join
     9 ******* formatting - printf style
     9 ******* formatting - printf style
       
    10 ***** Simple IO
       
    11 ******* raw_input
       
    12 ******* console output
    10 ***** Odds and Ends
    13 ***** Odds and Ends
    11 ******* dynamic typing
    14 ******* dynamic typing
    12 ******* comments
    15 ******* comments
    13 ***** Simple IO
       
    14 ******* raw_input
       
    15 ******* console output
       
    16 ***** Arsenal Required
    16 ***** Arsenal Required
    17 ******* lists
    17 ******* lists
    18 ******* writing to files
    18 ******* writing to files
    19 *** Script
    19 *** Script
    20     Welcome friends. 
    20     Welcome friends. 
    21     
    21     
    22     In this tutorial we shall look at data types available in Python and 
    22     In the previous tutorial we have looked at data types for dealing
    23     how to perform simple Input and Output operations. 
    23     with numbers. In this tutorial we shall look at strings. We shall
    24     for 'Numbers' we have: int, float, complex datatypes
    24     look at how to do elementary string manipulation, and simple input
    25     for Text content we have strings.
    25     and output operations. 
    26     For conditional statements, 'Booleans'.
       
    27     
    26     
    28     Now we shall look at Python Strings.
    27     As, we have seen in previous tutorials, anything enclosed within
    29     In python anything enclosed inside quotes(single or double) is a string
    28     quotes is a string. For example -
    30     so 
    29 
    31     a = 'This is a string'
    30     a = 'This is a string'
    32     print a
    31     print a
    33     b = "This too!"
    32     b = "This too!"
    34     print b
    33     print b
       
    34 
       
    35     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
       
    37     quotes. 
       
    38 
    35     c = '''This one too!'''
    39     c = '''This one too!'''
    36     print c
    40     print c
    37     d = """And one more."""
    41     d = """And one more."""
    38     print d
    42     print d
       
    43 
       
    44     These are special type of strings, called docstrings, which shall
       
    45     be discussed along with functions. 
    39     
    46     
    40     Similar to lists we covered earlier even string elements can be accessed 
    47     Like lists, which we already saw, string elements can be accessed
    41     via index numbers starting from 0
    48     with their indexes. The indexing here, also, begins from 0. 
    42 
    49 
    43     print a[0]    
    50     print a[0]    
    44     print a[5]
    51     print a[5]
    45     will 
    52 
    46     To access last element we can use a[-1] which is one of Pythons feature.
    53     To access the last element, we can use -1 as the index!
    47     print a[-1]
    54     print a[-1]
    48     len function works with the strings also as it does with the arrays and 
    55     Similarly, we could access other elements with corresponding -ve
    49     returns length of the string.
    56     indexes. This is a very handy feature of python. 
       
    57 
       
    58     The len function, which we used with lists, works with strings too. 
       
    59     len(a)
       
    60 
       
    61     Python's strings support the operations + and *
       
    62     a + b
       
    63     a * 4
       
    64     What do you think would happen when you do a * a?
       
    65     It's obviously an error since, it doesn't make any logical sense. 
    50     
    66     
    51     One thing to notice about the string variables is that they are 
    67     One thing to note about strings, is that they are immutable, that
    52     immutable, that is
    68     is 
    53     a[0] = 't'
    69     a[0] = 't'
    54     will throw an error
    70     throws an error
    55     
    71     
    56     Some of methods available for string are:
    72     Then how does one go about changing strings? Python provides
       
    73     'methods' for doing various manipulations on strings. For example - 
       
    74 
       
    75     a.upper() returns a string with all letters capitalized.
       
    76 
       
    77     and a.lower() returns a string with all smaller case letters.
       
    78 
    57     a.startswith('Thi')
    79     a.startswith('Thi')
    58     returns true if initial of the string is same
    80     returns True if the string starts with the argument passed. 
    59     similarly there is endswith
    81 
       
    82     similarly there's endswith
    60     a.endswith('ING')
    83     a.endswith('ING')
    61     a.upper() returns a string with all letters capitalized.
    84 
    62     and a.lower() returns a string with all smaller case letters.
    85     We've seen the use of split function in the previous
    63     As we have seen earlier use of split function, it returns the list after
    86     tutorials. split returns a list after splitting the string on the
    64     splitting the string, so
    87     given argument. 
    65     a.split()
    88     alist = a.split()
    66     will give list with three elements.
    89     will give list with four elements.
    67     we also have 'join' function, which does the opposite of what
    90     print alist
       
    91 
       
    92     Python also has a 'join' function, which does the opposite of what
    68     split does. 
    93     split does. 
    69     ''.join(['a','b','c']) will return a joined string of the list we pass
    94     ' '.join(alist) will return the original string a. 
    70     to it. Since join is performed on '' that is empty string we get 'abc'
    95     '-'.join(alist) will return a string with the spaces in the string
    71     if we do something like
    96     'a' replaced with hyphens. 
    72     '-'.join(['a','b','c'])
    97 
       
    98     * formatting - printf style *
       
    99       In []: x, y = 1, 1.234
       
   100       
       
   101       In []: 'x is %s, y is %s' %(x, y)
       
   102       Out[]: 'x is 1, y is 1.234'
       
   103     * formatting - printf style *
       
   104 
       
   105 
       
   106     Now we shall look at simple input from and output to the
       
   107     console. 
       
   108     The raw_input function allows us to give input from the console. 
       
   109     a = raw_input()
       
   110     it is now waiting for the user input. 
       
   111     5
       
   112     a
       
   113     raw_input also allows us to give a prompt string, as shown 
       
   114     a = raw_input("Enter a value: ")
       
   115     Enter a value: 5
       
   116     Note that a, is now a string variable and not an integer. 
       
   117     type(a)
       
   118     we could use type conversion similar to that shown in the tutorial
       
   119     on numeric datatypes. 
       
   120     a = int(a)
       
   121     a has now been converted to an integer. 
       
   122     type(a)
       
   123 
       
   124     For console output, we use print which is pretty straightforward. 
       
   125     We shall look at a subtle feature of the print statement. 
       
   126     We shall first put the following code snippet in the file
       
   127     "hello1.py"
       
   128     print "Hello"
       
   129     print "World"
       
   130     We save the file and run it from the ipython interpreter. Make
       
   131     sure you navigate to the place, where you have saved it. 
       
   132     %run -i hello1.py
       
   133 
       
   134     Now we make a small change to the code snippet and save it in the
       
   135     file named "hello2.py"
       
   136     print "Hello", 
       
   137     print "World"
       
   138     We now run this file, from the ipython interpreter. 
       
   139     %run -i hello2.py
       
   140     Note the difference in the output of the two files that we
       
   141     executed. The comma adds a space at the end of the line, instead
       
   142     of a new line character that is normally added. 
       
   143 
       
   144     Before we wind up, a couple of miscellaneous things. 
       
   145     As you may have already noticed, Python is a dynamically typed
       
   146     language, that is you don't have to specify the type of a variable
       
   147     when using a new one. You don't have to do anything special, to use
       
   148     a variable that was of int type as a float or string. 
    73     
   149     
    74     we come to the end of this tutorial on introduction of Data types in
   150     a = 1
       
   151     a = 1.1
       
   152     a = "Now I am a string!"
       
   153 
       
   154     Comments in Python start with a pound or hash sign. Anything after
       
   155     a #, until the end of the line is considered a comment, except of
       
   156     course, if the hash is in a string. 
       
   157     a = 1 # in-line comments
       
   158     # a comment line
       
   159     a = "# not a comment"
       
   160 
       
   161     we come to the end of this tutorial on strings introduction of Data types in
    75     Python. In this tutorial we have learnt what are supported data types, 
   162     Python. In this tutorial we have learnt what are supported data types, 
    76     supported operations and performing simple IO operations in Python.
   163     supported operations and performing simple IO operations in Python.
    77 
   164 
    78 *** Notes
   165 *** Notes