strings.org
changeset 95 fddcfd83e4f0
child 96 3498d74ed615
equal deleted inserted replaced
94:57ae1f75b7e0 95:fddcfd83e4f0
       
     1 * Data Types
       
     2 *** Outline
       
     3 ***** Introduction
       
     4 ******* What are we going to do?
       
     5 ******* How are we going to do?
       
     6 ******* Arsenal Required
       
     7 ********* None
       
     8 *** Script
       
     9     Welcome friends. 
       
    10     
       
    11     In this tutorial we shall look at data types available in Python and 
       
    12     how to perform simple Input and Output operations. 
       
    13     for 'Numbers' we have: int, float, complex datatypes
       
    14     for Text content we have strings.
       
    15     For conditional statements, 'Booleans'.
       
    16     
       
    17     Now we shall look at Python Strings.
       
    18     In python anything enclosed inside quotes(single or double) is a string
       
    19     so 
       
    20     a = 'This is a string'
       
    21     print a
       
    22     b = "This too!"
       
    23     print b
       
    24     c = '''This one too!'''
       
    25     print c
       
    26     d = """And one more."""
       
    27     print d
       
    28     
       
    29     Similar to lists we covered earlier even string elements can be accessed 
       
    30     via index numbers starting from 0
       
    31 
       
    32     print a[0]    
       
    33     print a[5]
       
    34     will 
       
    35     To access last element we can use a[-1] which is one of Pythons feature.
       
    36     print a[-1]
       
    37     len function works with the strings also as it does with the arrays and 
       
    38     returns length of the string.
       
    39     
       
    40     One thing to notice about the string variables is that they are 
       
    41     immutable, that is
       
    42     a[0] = 't'
       
    43     will throw an error
       
    44     
       
    45     Some of methods available for string are:
       
    46     a.startswith('Thi')
       
    47     returns true if initial of the string is same
       
    48     similarly there is endswith
       
    49     a.endswith('ING')
       
    50     a.upper() returns a string with all letters capitalized.
       
    51     and a.lower() returns a string with all smaller case letters.
       
    52     As we have seen earlier use of split function, it returns the list after
       
    53     splitting the string, so
       
    54     a.split()
       
    55     will give list with three elements.
       
    56     we also have 'join' function, which does the opposite of what
       
    57     split does. 
       
    58     ''.join(['a','b','c']) will return a joined string of the list we pass
       
    59     to it. Since join is performed on '' that is empty string we get 'abc'
       
    60     if we do something like
       
    61     '-'.join(['a','b','c'])
       
    62     
       
    63     we come to the end of this tutorial on introduction of Data types in
       
    64     Python. In this tutorial we have learnt what are supported data types, 
       
    65     supported operations and performing simple IO operations in Python.
       
    66 
       
    67 *** Notes