getting-started-strings.rst
changeset 313 b9b7bfce773e
parent 312 8cb703eee88d
child 314 11869b16d86b
equal deleted inserted replaced
312:8cb703eee88d 313:b9b7bfce773e
     1 Hello friends. Welcome to this spoken tutorial on Getting started with
       
     2 strings.
       
     3 
       
     4 {{{ Show the slide containing the title }}}
       
     5 
       
     6 {{{ Show the slide containing the outline }}}
       
     7 
       
     8 In this tutorial, we will learn what do we actually mean by strings in
       
     9 python, how python supports the use of strings. We will also learn
       
    10 some of the operations that can be performed on strings.
       
    11 
       
    12 {{{ Shift to terminal and start ipython }}}
       
    13 
       
    14 To begin with let us start ipython, by typing::
       
    15 
       
    16   ipython
       
    17 
       
    18 on the terminal
       
    19 
       
    20 So what are strings? In Python anything within either single quotes
       
    21 or double quotes or triple single quotes or triple double quotes are
       
    22 strings. This is true whatsoever, even if there is only one character
       
    23 within the quotes
       
    24 
       
    25 {{{ Type in ipython the following and read them as you type }}}::
       
    26 
       
    27   'This is a string'
       
    28   "This is a string too'
       
    29   '''This is a string as well'''
       
    30   """This is also a string"""
       
    31   'p'
       
    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 
       
    52 We can assign this string to any variable::
       
    53 
       
    54   a = 'Hello, World!'
       
    55 
       
    56 Now 'a' is a string variable. String is a collection of characters. In
       
    57 addition string is an immutable collection. So all the operations that
       
    58 are applicable to any other immutable collection in Python works on
       
    59 string as well. So we can add two strings::
       
    60 
       
    61   a = 'Hello'
       
    62   b = 'World'
       
    63   c = a + ', ' + b + '!'
       
    64 
       
    65 We can add string variables as well as the strings themselves all in
       
    66 the same statement. The addition operation performs the concatenation
       
    67 of two strings.
       
    68 
       
    69 Similarly we can multiply a string with an integer::
       
    70 
       
    71   a = 'Hello'
       
    72   a * 5
       
    73 
       
    74 gives another string in which the original string 'Hello' is repeated
       
    75 5 times.
       
    76 
       
    77 Since strings are collections we can access individual items in the
       
    78 string using the subscripts::
       
    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.
       
   102 
       
   103 {{{ Show summary slide }}}
       
   104 
       
   105 This brings us to the end of another session. In this tutorial session
       
   106 we learnt
       
   107 
       
   108   * How to define strings
       
   109   * Different types of defining a string
       
   110   * String concatenation and repeatition
       
   111   * Accessing individual elements of the string
       
   112   * Immutability of strings
       
   113 
       
   114 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   115 
       
   116 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   117 
       
   118 Hope you have enjoyed and found it useful.
       
   119 Thankyou
       
   120  
       
   121 .. Author              : Madhu
       
   122    Internal Reviewer 1 :         [potential reviewer: Nishanth]
       
   123    Internal Reviewer 2 :         [potential reviewer: Amit]
       
   124    External Reviewer   :
       
   125