getting-started-strings.rst
author Nishanth <nishanth@fossee.in>
Fri, 08 Oct 2010 23:55:07 +0530
changeset 256 a3aa223c1662
parent 167 f22530911c51
permissions -rw-r--r--
added script to using_sage_to_teach
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
166
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
Hello friends. Welcome to this spoken tutorial on Getting started with
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
strings.
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
{{{ Show the slide containing the title }}}
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
{{{ Show the slide containing the outline }}}
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
In this tutorial, we will learn what do we actually mean by strings in
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
python, how python supports the use of strings. We will also learn
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
some of the operations that can be performed on strings.
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
{{{ Shift to terminal and start ipython }}}
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
To begin with let us start ipython, by typing::
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
  ipython
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
on the terminal
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
So what are strings? In Python anything within either single quotes
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
or double quotes or triple single quotes or triple double quotes are
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
strings. This is true whatsoever, even if there is only one character
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
within the quotes
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
{{{ Type in ipython the following and read them as you type }}}::
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
  'This is a string'
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    28
  "This is a string too'
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    29
  '''This is a string as well'''
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    30
  """This is also a string"""
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    31
  'p'
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    32
167
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    33
Having more than one control character to define strings come as very
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    34
handy when one of the control characters itself is part of the
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    35
string. For example::
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    36
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    37
  "Python's string manipulation functions are very useful"
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    38
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    39
In this case we use single quote for apostrophe. If we had only single
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    40
quote to define strings we should have a clumsy way of escaping the
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    41
single quote character to make it part of the string. Hence this is a
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    42
very handy feature.
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    43
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    44
The triple quoted strings let us define multi-lines strings without
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    45
using any escaping. Everything within the triple quotes is a single
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    46
string no matter how many lines it extends::
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    47
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    48
   """Having more than one control character to define
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    49
   strings come as very handy when one of the control
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    50
   characters itself is part of the string."""
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    51
166
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    52
We can assign this string to any variable::
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    53
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    54
  a = 'Hello, World!'
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    55
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    56
Now 'a' is a string variable. String is a collection of characters. In
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    57
addition string is an immutable collection. So all the operations that
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    58
are applicable to any other immutable collection in Python works on
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    59
string as well. So we can add two strings::
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    60
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    61
  a = 'Hello'
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    62
  b = 'World'
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    63
  c = a + ', ' + b + '!'
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    64
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    65
We can add string variables as well as the strings themselves all in
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    66
the same statement. The addition operation performs the concatenation
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    67
of two strings.
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    68
167
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    69
Similarly we can multiply a string with an integer::
166
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    70
167
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    71
  a = 'Hello'
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    72
  a * 5
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    73
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    74
gives another string in which the original string 'Hello' is repeated
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    75
5 times.
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    76
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    77
Since strings are collections we can access individual items in the
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    78
string using the subscripts::
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    79
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    80
  a[0]
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    81
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    82
gives us the first character in the string. The indexing starts from 0
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    83
for the first character up to n-1 for the last character. We can
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    84
access the strings from the end using negative indices::
166
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    85
167
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    86
  a[-2]
166
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    87
167
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    88
gives us second element from the end of the string
166
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    89
167
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    90
Let us attempt to change one of the characters in a string::
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    91
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    92
  a = 'hello'
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    93
  a[0] = 'H'
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    94
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    95
As said earlier, strings are immutable. We cannot manipulate the
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    96
string. Although there are some methods which let us to manipulate the
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    97
strings. We will look at them in the advanced session on strings. In
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    98
addition to the methods that let us manipulate the strings we have
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
    99
methods like split which lets us break the string on the specified
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
   100
separator, the join method which lets us combine the list of strings
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
   101
into a single string based on the specified separator.
166
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   102
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   103
{{{ Show summary slide }}}
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   104
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   105
This brings us to the end of another session. In this tutorial session
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   106
we learnt
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   107
167
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
   108
  * How to define strings
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
   109
  * Different types of defining a string
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
   110
  * String concatenation and repeatition
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
   111
  * Accessing individual elements of the string
f22530911c51 Completed session on getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 166
diff changeset
   112
  * Immutability of strings
166
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   113
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   114
{{{ Show the "sponsored by FOSSEE" slide }}}
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   115
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   116
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   117
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   118
Hope you have enjoyed and found it useful.
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   119
Thankyou
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   120
 
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   121
.. Author              : Madhu
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   122
   Internal Reviewer 1 :         [potential reviewer: Nishanth]
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   123
   Internal Reviewer 2 :         [potential reviewer: Amit]
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   124
   External Reviewer   :
cfae14bb6809 Adding an uncompleted version of getting started with strings.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
   125