strings.org
author Shantanu <shantanu@fossee.in>
Thu, 22 Apr 2010 13:08:06 +0530
changeset 102 84e1dcb52908
parent 100 47a2ba7beaf8
child 103 587eb2416e6c
permissions -rw-r--r--
changes to numbers org file.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
96
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
     1
* Strings
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
     2
*** Outline
96
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
     3
***** Strings
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
     4
******* basic manipulation
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
     5
******* operations
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
     6
******* immutability
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
     7
******* string methods
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
     8
******* split and join
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
     9
******* formatting - printf style
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    10
***** Simple IO
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    11
******* raw_input
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    12
******* console output
96
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
    13
***** Odds and Ends
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
    14
******* dynamic typing
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
    15
******* comments
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
    16
***** Arsenal Required
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
    17
******* lists
3498d74ed615 Changed outline of strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 95
diff changeset
    18
******* writing to files
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    19
*** Script
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    20
    Welcome friends. 
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    21
    
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    22
    In the previous tutorial we have looked at data types for dealing
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    23
    with numbers. In this tutorial we shall look at strings. We shall
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    24
    look at how to do elementary string manipulation, and simple input
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    25
    and output operations. 
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    26
    
100
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
    27
    In Python anything enclosed within quotes is a string. Lets get 
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
    28
    started by starting ipython interpreter. We shall create some 
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
    29
    string variables by:
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    30
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    31
    a = 'This is a string'
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    32
    print a
100
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
    33
    type(a) shows it is 'str'
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    34
    b = "This too!"
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    35
    print b
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    36
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    37
    They could either be enclosed in single or double quotes. There is
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    38
    also a special type of string enclosed in triple single or double
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    39
    quotes. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    40
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    41
    c = '''This one too!'''
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    42
    print c
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    43
    d = """And one more."""
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    44
    print d
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    45
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    46
    These are special type of strings, called docstrings, which shall
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    47
    be discussed along with functions. 
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    48
    
100
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
    49
    Like lists and arrays, which we have already seen, string elements 
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
    50
    can also be accessed with their indexes. The indexing here, also, 
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
    51
    begins from 0. 
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    52
100
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
    53
    print a[0] gives us 'T'
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
    54
    print a[5] gives us 'i' which is 6th character.
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    55
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    56
    To access the last element, we can use -1 as the index!
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    57
    print a[-1]
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    58
    Similarly, we could access other elements with corresponding -ve
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    59
    indexes. This is a very handy feature of python. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    60
99
0bc1c9ec4fcf Changes to strings script.
Shantanu <shantanu@fossee.in>
parents: 97
diff changeset
    61
    The len function, which we used with lists and arrays, works with
0bc1c9ec4fcf Changes to strings script.
Shantanu <shantanu@fossee.in>
parents: 97
diff changeset
    62
    strings too. 
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    63
    len(a)
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    64
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    65
    Python's strings support the operations + and *
100
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
    66
    + concatenates two strings.
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    67
    a + b
100
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
    68
    and * is used for replicating a string for given number of times.
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    69
    a * 4
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    70
    What do you think would happen when you do a * a?
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    71
    It's obviously an error since, it doesn't make any logical sense. 
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    72
    
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    73
    One thing to note about strings, is that they are immutable, that
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    74
    is 
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    75
    a[0] = 't'
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    76
    throws an error
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    77
    
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    78
    Then how does one go about changing strings? Python provides
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    79
    'methods' for doing various manipulations on strings. For example - 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    80
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    81
    a.upper() returns a string with all letters capitalized.
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    82
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    83
    and a.lower() returns a string with all smaller case letters.
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    84
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    85
    a.startswith('Thi')
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    86
    returns True if the string starts with the argument passed. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    87
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    88
    similarly there's endswith
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    89
    a.endswith('ING')
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    90
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    91
    We've seen the use of split function in the previous
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    92
    tutorials. split returns a list after splitting the string on the
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    93
    given argument. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    94
    alist = a.split()
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    95
    will give list with four elements.
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    96
    print alist
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
    98
    Python also has a 'join' function, which does the opposite of what
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
    99
    split does. 
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   100
    ' '.join(alist) will return the original string a. 
100
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
   101
    This function takes list of elements(in our case alist) to be joined.
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   102
    '-'.join(alist) will return a string with the spaces in the string
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   103
    'a' replaced with hyphens. 
99
0bc1c9ec4fcf Changes to strings script.
Shantanu <shantanu@fossee.in>
parents: 97
diff changeset
   104
    
0bc1c9ec4fcf Changes to strings script.
Shantanu <shantanu@fossee.in>
parents: 97
diff changeset
   105
    At times we want our output or message in a particular
0bc1c9ec4fcf Changes to strings script.
Shantanu <shantanu@fossee.in>
parents: 97
diff changeset
   106
    format with variables embedded, something like printf in C. For 
0bc1c9ec4fcf Changes to strings script.
Shantanu <shantanu@fossee.in>
parents: 97
diff changeset
   107
    those situations python provides a provision. First lets create some 
100
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
   108
    variables say
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   109
    * formatting - printf style *
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   110
      In []: x, y = 1, 1.234
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   111
      
99
0bc1c9ec4fcf Changes to strings script.
Shantanu <shantanu@fossee.in>
parents: 97
diff changeset
   112
      In []: print 'x is %s, y is %s' %(x, y)
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   113
      Out[]: 'x is 1, y is 1.234'
99
0bc1c9ec4fcf Changes to strings script.
Shantanu <shantanu@fossee.in>
parents: 97
diff changeset
   114
      Here %s means string, you can also try %d or %f for integer and 
100
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
   115
      float values respectively.
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   116
    * formatting - printf style *
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   117
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   118
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   119
    Now we shall look at simple input from and output to the
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   120
    console. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   121
    The raw_input function allows us to give input from the console. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   122
    a = raw_input()
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   123
    it is now waiting for the user input. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   124
    5
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   125
    a
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   126
    raw_input also allows us to give a prompt string, as shown 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   127
    a = raw_input("Enter a value: ")
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   128
    Enter a value: 5
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   129
    Note that a, is now a string variable and not an integer. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   130
    type(a)
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   131
    we could use type conversion similar to that shown in the tutorial
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   132
    on numeric datatypes. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   133
    a = int(a)
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   134
    a has now been converted to an integer. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   135
    type(a)
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   136
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   137
    For console output, we use print which is pretty straightforward. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   138
    We shall look at a subtle feature of the print statement. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   139
    We shall first put the following code snippet in the file
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   140
    "hello1.py"
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   141
    print "Hello"
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   142
    print "World"
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   143
    We save the file and run it from the ipython interpreter. Make
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   144
    sure you navigate to the place, where you have saved it. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   145
    %run -i hello1.py
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   146
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   147
    Now we make a small change to the code snippet and save it in the
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   148
    file named "hello2.py"
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   149
    print "Hello", 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   150
    print "World"
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   151
    We now run this file, from the ipython interpreter. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   152
    %run -i hello2.py
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   153
    Note the difference in the output of the two files that we
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   154
    executed. The comma adds a space at the end of the line, instead
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   155
    of a new line character that is normally added. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   156
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   157
    Before we wind up, a couple of miscellaneous things. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   158
    As you may have already noticed, Python is a dynamically typed
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   159
    language, that is you don't have to specify the type of a variable
100
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
   160
    when using a new one. You don't have to do anything special, to 'reuse'
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   161
    a variable that was of int type as a float or string. 
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
   162
    
100
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
   163
    a = 1 here a is integer
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
   164
    a = 1.1 now a float
97
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   165
    a = "Now I am a string!"
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   166
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   167
    Comments in Python start with a pound or hash sign. Anything after
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   168
    a #, until the end of the line is considered a comment, except of
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   169
    course, if the hash is in a string. 
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   170
    a = 1 # in-line comments
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   171
    # a comment line
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   172
    a = "# not a comment"
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   173
25248b12f6e4 Major changes to strings.org.
Puneeth Chaganti <punchagan@gmail.com>
parents: 96
diff changeset
   174
    we come to the end of this tutorial on strings introduction of Data types in
100
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
   175
    Python. In this tutorial we have learnt what are supported operations and 
47a2ba7beaf8 Added strings presentation.
Shantanu <shantanu@fossee.in>
parents: 99
diff changeset
   176
    performing simple IO operations in Python.
95
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
   177
fddcfd83e4f0 Split data-files.org into numbers and strings.
Puneeth Chaganti <punchagan@gmail.com>
parents:
diff changeset
   178
*** Notes