getting-started-strings/script.rst
author bhanu
Tue, 16 Nov 2010 23:26:42 +0530
changeset 517 71697b10f4ae
parent 515 0eaf233cb64f
permissions -rw-r--r--
Language check done for `manipulating strings`
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     1
.. Objectives
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     2
.. ----------
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     3
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     4
.. At the end of this tutorial, you should know --
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     5
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     6
..   1. How to define strings
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     7
..   #. Different ways of defining a string
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     8
..   #. How to concatenate strings 
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     9
..   #. How to print a string repeatedly 
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    10
..   #. Accessing individual elements of the string
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    11
..   #. Immutability of strings
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    12
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    13
.. Prerequisites
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    14
.. -------------
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    15
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    16
.. 1. getting started with ipython
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    17
     
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    18
.. Author              : Madhu
513
c1f89e89d89c Language check done for `getting started with strings`
bhanu
parents: 436
diff changeset
    19
   Internal Reviewer   : Punch
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    20
   External Reviewer   :
513
c1f89e89d89c Language check done for `getting started with strings`
bhanu
parents: 436
diff changeset
    21
   Language Reviewer   : Bhanukiran
515
0eaf233cb64f checklist ok
anand
parents: 514
diff changeset
    22
   Checklist OK?       : <15-11-2010, Anand, OK> [2010-10-05]
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    23
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    24
Script
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    25
------
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    26
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    27
{{{ Show the slide containing the title }}}
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    28
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    29
Hello friends. Welcome to this spoken tutorial on Getting started with
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    30
strings.
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    31
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
{{{ Show the slide containing the outline }}}
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
436
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    34
In this tutorial, we will look at what we really mean by strings, how
514
ca9d084d0c87 checklist ok for `getting started with strings`
anand
parents: 513
diff changeset
    35
Python supports the use of strings and some of the operations that can
436
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    36
be performed on strings. 
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    37
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
{{{ Shift to terminal and start ipython }}}
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    40
To begin with let us start ipython, by typing::
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    41
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    42
  ipython
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    44
on the terminal
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
436
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    46
So, what are strings? In Python anything within either single quotes
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
or double quotes or triple single quotes or triple double quotes are
436
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    48
strings. 
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    50
{{{ Type in ipython the following and read them as you type }}}::
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    51
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    52
  'This is a string'
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
  "This is a string too'
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
  '''This is a string as well'''
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    55
  """This is also a string"""
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    56
  'p'
436
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    57
  ""
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    58
436
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    59
Note that it really doesn't matter how many characters are present in
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    60
the string. The last example is a null string or an empty string. 
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    61
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    62
Having more than one control character to define strings is handy when
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    63
one of the control characters itself is part of the string. For
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    64
example::
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    65
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    66
  "Python's string manipulation functions are very useful"
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    67
436
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    68
By having multiple control characters, we avoid the need for
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    69
escaping characters -- in this case the apostrophe. 
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    70
436
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
    71
The triple quoted strings let us define multi-line strings without
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    72
using any escaping. Everything within the triple quotes is a single
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    73
string no matter how many lines it extends::
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    74
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    75
   """Having more than one control character to define
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    76
   strings come as very handy when one of the control
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    77
   characters itself is part of the string."""
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    78
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    79
We can assign this string to any variable::
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    80
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    81
  a = 'Hello, World!'
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    82
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    83
Now 'a' is a string variable. String is a collection of characters. In
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    84
addition string is an immutable collection. So all the operations that
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    85
are applicable to any other immutable collection in Python works on
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    86
string as well. So we can add two strings::
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    87
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    88
  a = 'Hello'
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    89
  b = 'World'
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    90
  c = a + ', ' + b + '!'
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    91
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    92
We can add string variables as well as the strings themselves all in
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    93
the same statement. The addition operation performs the concatenation
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    94
of two strings.
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    95
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    96
Similarly we can multiply a string with an integer::
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    97
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    98
  a = 'Hello'
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    99
  a * 5
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   100
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   101
gives another string in which the original string 'Hello' is repeated
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
5 times.
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   103
436
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   104
Following is an exercise that you must do. 
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   105
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   106
%% %% Obtain the string ``%% -------------------- %%`` (20 hyphens)
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   107
      without typing out all the twenty hyphens. 
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   108
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   109
Please, pause the video here. Do the exercise and then continue. 
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   110
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   111
::
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   112
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   113
  s = "%% " + "-"*20 + " %%"
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   114
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   115
Let's now look at accessing individual elements of strings. Since,
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   116
strings are collections we can access individual items in the string
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   117
using the subscripts::
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   118
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   119
  a[0]
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   120
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   121
gives us the first character in the string. The indexing starts from 0
436
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   122
for the first character and goes up to n-1 for the last character. We
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   123
can access the strings from the end using negative indices::
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   124
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   125
  a[-1]
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   126
436
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   127
gives us the last element of the string and 
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   128
::
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   129
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   130
    a[-2]
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   131
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   132
gives us second element from the end of the string
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   133
436
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   134
Following is an exercise that you must do. 
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   135
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   136
%% %% Given a string, ``s = "Hello World"``, what is the output of::
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   137
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   138
      s[-5] 
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   139
      s[-10]
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   140
      s[-15]
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   141
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   142
Please, pause the video here. Do the exercise and then continue. 
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   143
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   144
::
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   145
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   146
  s[-5] 
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   147
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   148
gives us 'W'
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   149
::
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   150
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   151
  s[-10] 
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   152
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   153
gives us 'e' and 
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   154
::
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   155
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   156
  s[-15] 
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   157
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   158
gives us an ``IndexError``, as should be expected, since the string
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   159
given to us is only 11 characters long. 
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   160
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   161
Let us attempt to change one of the characters in a string::
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   162
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   163
  a = 'hello'
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   164
  a[0] = 'H'
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   165
513
c1f89e89d89c Language check done for `getting started with strings`
bhanu
parents: 436
diff changeset
   166
As said earlier, strings are immutable. We cannot manipulate a
c1f89e89d89c Language check done for `getting started with strings`
bhanu
parents: 436
diff changeset
   167
string. Although there are some methods which let us manipulate
517
71697b10f4ae Language check done for `manipulating strings`
bhanu
parents: 515
diff changeset
   168
strings, we will look at them in the advanced session on strings. In
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   169
addition to the methods that let us manipulate the strings we have
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   170
methods like split which lets us break the string on the specified
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
separator, the join method which lets us combine the list of strings
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
into a single string based on the specified separator.
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   173
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
{{{ Show summary slide }}}
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   175
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   176
This brings us to the end of another session. In this tutorial session
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   177
we learnt
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   178
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   179
  * How to define strings
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   180
  * Different ways of defining a string
436
e62bc810999c Cleaned up getting started with strings LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 313
diff changeset
   181
  * String concatenation and repetition
313
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   182
  * Accessing individual elements of the string
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   183
  * Immutability of strings
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   184
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   185
{{{ Show the "sponsored by FOSSEE" slide }}}
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   186
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   187
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   188
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   189
Hope you have enjoyed and found it useful.
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   190
Thank you!
b9b7bfce773e Getting started with strings LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   191