strings.org
changeset 99 0bc1c9ec4fcf
parent 97 25248b12f6e4
child 100 47a2ba7beaf8
equal deleted inserted replaced
98:8e02b76cf068 99:0bc1c9ec4fcf
    53     To access the last element, we can use -1 as the index!
    53     To access the last element, we can use -1 as the index!
    54     print a[-1]
    54     print a[-1]
    55     Similarly, we could access other elements with corresponding -ve
    55     Similarly, we could access other elements with corresponding -ve
    56     indexes. This is a very handy feature of python. 
    56     indexes. This is a very handy feature of python. 
    57 
    57 
    58     The len function, which we used with lists, works with strings too. 
    58     The len function, which we used with lists and arrays, works with
       
    59     strings too. 
    59     len(a)
    60     len(a)
    60 
    61 
    61     Python's strings support the operations + and *
    62     Python's strings support the operations + and *
    62     a + b
    63     a + b
    63     a * 4
    64     a * 4
    92     Python also has a 'join' function, which does the opposite of what
    93     Python also has a 'join' function, which does the opposite of what
    93     split does. 
    94     split does. 
    94     ' '.join(alist) will return the original string a. 
    95     ' '.join(alist) will return the original string a. 
    95     '-'.join(alist) will return a string with the spaces in the string
    96     '-'.join(alist) will return a string with the spaces in the string
    96     'a' replaced with hyphens. 
    97     'a' replaced with hyphens. 
    97 
    98     
       
    99     At times we want our output or message in a particular
       
   100     format with variables embedded, something like printf in C. For 
       
   101     those situations python provides a provision. First lets create some 
       
   102     variables
    98     * formatting - printf style *
   103     * formatting - printf style *
    99       In []: x, y = 1, 1.234
   104       In []: x, y = 1, 1.234
   100       
   105       
   101       In []: 'x is %s, y is %s' %(x, y)
   106       In []: print 'x is %s, y is %s' %(x, y)
   102       Out[]: 'x is 1, y is 1.234'
   107       Out[]: 'x is 1, y is 1.234'
       
   108       Here %s means string, you can also try %d or %f for integer and 
       
   109       float values.
   103     * formatting - printf style *
   110     * formatting - printf style *
   104 
   111 
   105 
   112 
   106     Now we shall look at simple input from and output to the
   113     Now we shall look at simple input from and output to the
   107     console. 
   114     console.