getting-started-strings.rst
changeset 167 f22530911c51
parent 166 cfae14bb6809
equal deleted inserted replaced
166:cfae14bb6809 167:f22530911c51
     1 3.3 LO: getting started with strings (2) [madhu] 
       
     2 -------------------------------------------------
       
     3 * strings 
       
     4   + single, double, triple quoted 
       
     5 * accessing elements 
       
     6 * show immutability 
       
     7 * tell that there are methods for manipulation 
       
     8 
       
     9 
       
    10 Hello friends. Welcome to this spoken tutorial on Getting started with
     1 Hello friends. Welcome to this spoken tutorial on Getting started with
    11 strings.
     2 strings.
    12 
     3 
    13 {{{ Show the slide containing the title }}}
     4 {{{ Show the slide containing the title }}}
    14 
     5 
    37   "This is a string too'
    28   "This is a string too'
    38   '''This is a string as well'''
    29   '''This is a string as well'''
    39   """This is also a string"""
    30   """This is also a string"""
    40   'p'
    31   'p'
    41 
    32 
       
    33 Having more than one control character to define strings come as very
       
    34 handy when one of the control characters itself is part of the
       
    35 string. For example::
       
    36 
       
    37   "Python's string manipulation functions are very useful"
       
    38 
       
    39 In this case we use single quote for apostrophe. If we had only single
       
    40 quote to define strings we should have a clumsy way of escaping the
       
    41 single quote character to make it part of the string. Hence this is a
       
    42 very handy feature.
       
    43 
       
    44 The triple quoted strings let us define multi-lines strings without
       
    45 using any escaping. Everything within the triple quotes is a single
       
    46 string no matter how many lines it extends::
       
    47 
       
    48    """Having more than one control character to define
       
    49    strings come as very handy when one of the control
       
    50    characters itself is part of the string."""
       
    51 
    42 We can assign this string to any variable::
    52 We can assign this string to any variable::
    43 
    53 
    44   a = 'Hello, World!'
    54   a = 'Hello, World!'
    45 
    55 
    46 Now 'a' is a string variable. String is a collection of characters. In
    56 Now 'a' is a string variable. String is a collection of characters. In
    54 
    64 
    55 We can add string variables as well as the strings themselves all in
    65 We can add string variables as well as the strings themselves all in
    56 the same statement. The addition operation performs the concatenation
    66 the same statement. The addition operation performs the concatenation
    57 of two strings.
    67 of two strings.
    58 
    68 
       
    69 Similarly we can multiply a string with an integer::
    59 
    70 
       
    71   a = 'Hello'
       
    72   a * 5
    60 
    73 
       
    74 gives another string in which the original string 'Hello' is repeated
       
    75 5 times.
    61 
    76 
       
    77 Since strings are collections we can access individual items in the
       
    78 string using the subscripts::
    62 
    79 
       
    80   a[0]
       
    81 
       
    82 gives us the first character in the string. The indexing starts from 0
       
    83 for the first character up to n-1 for the last character. We can
       
    84 access the strings from the end using negative indices::
       
    85 
       
    86   a[-2]
       
    87 
       
    88 gives us second element from the end of the string
       
    89 
       
    90 Let us attempt to change one of the characters in a string::
       
    91 
       
    92   a = 'hello'
       
    93   a[0] = 'H'
       
    94 
       
    95 As said earlier, strings are immutable. We cannot manipulate the
       
    96 string. Although there are some methods which let us to manipulate the
       
    97 strings. We will look at them in the advanced session on strings. In
       
    98 addition to the methods that let us manipulate the strings we have
       
    99 methods like split which lets us break the string on the specified
       
   100 separator, the join method which lets us combine the list of strings
       
   101 into a single string based on the specified separator.
    63 
   102 
    64 {{{ Show summary slide }}}
   103 {{{ Show summary slide }}}
    65 
   104 
    66 This brings us to the end of another session. In this tutorial session
   105 This brings us to the end of another session. In this tutorial session
    67 we learnt
   106 we learnt
    68 
   107 
    69   *
   108   * How to define strings
    70 
   109   * Different types of defining a string
       
   110   * String concatenation and repeatition
       
   111   * Accessing individual elements of the string
       
   112   * Immutability of strings
    71 
   113 
    72 {{{ Show the "sponsored by FOSSEE" slide }}}
   114 {{{ Show the "sponsored by FOSSEE" slide }}}
    73 
   115 
    74 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
   116 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
    75 
   117