Main.Strings History

Hide minor edits - Show changes to markup

February 11, 2013, at 06:56 PM MST by 24.54.128.180 -
Added lines 1-160:

(:source lang=python:) """Strings. Material from Chapter 7 http://openbookproject.net/thinkcs/python/english2e/ch07.html """

  1. The bracket operator selects a single character from a string:

fruit = "banana" letter = fruit[1] print letter

  1. The expression fruit[1] selects the character with index 1 from fruit.
  2. When we display letter, we get a surprise:
  3. a
  4. The letter with index 1 is not the first letter!
  5. Computer scientists always start counting from zero.
  6. A nice explanation is given at:
  7. http://www.cs.utexas.edu/~EWD/ewd08xx/EWD831.PDF
  8. (the writer is Edsger W. Dijkstra, one of the founding fathers
  9. of computer science)
  10. The len function returns the number of characters in a string:

fruit = "banana" print len(fruit)

  1. Traversing the letters of a string can be done in two ways:

fruit = "banana" for i in range(len(fruit)) :

    print fruit[i]
  1. or

fruit = "banana" for character in fruit :

    print character
  1. we can extract a substring of a string using the bracket operator:

s = "python java perl" print s[0:6] print s[7:11] print s[12:len(s)]

  1. these are called slices
  2. some shortcuts:

print s[:6] # print a prefix of the string print s[12:] # print a suffix of the string

  1. what do you think s[:] would give?

print s[:]

def extract_every_2nd(s) :

    result = ''  # the empty string
    for i in range(0, len(s), 2) :
        result += s[i]
    return result
  1. It is tempting to use the bracket operator in order to modify
  2. a string.

greeting = "Hello, world!"

  1. greeting[0] = 'J' # ERROR!
  2. strings are immutable, i.e. cannot be changed once created.
  3. you will have to create a new string with the desired modification:

greeting = "Hello, world!" new_greeting = 'J' + greeting[1:] print new_greeting

  1. The in operator
  2. The in operator tests if one string is a substring of another:

'p' in 'apple' 'i' in 'apple' 'ap' in 'apple'

  1. Note that a string is a substring of itself:

'apple' in 'apple'

def remove_vowels(s) :

    """a function that removes the vowels from a string"""
    vowels = "aeiouAEIOU"
    s_without_vowels = ""
    for letter in s:
        if letter not in vowels:
            s_without_vowels += letter
    return s_without_vowels
  1. the in operator only determines if a string is a substring of
  2. another one. here's a function that also provides the index
  3. of the first occurrence:
  4. (the book gives a version with a while loop, and it only finds
  5. the location of a single character)

def find(s, substr) :

    for index in range(len(s)) :
        if s[index:index+len(substr)] == substr:
            return index
    return -1
  1. here's a version that searches in s[start:end]
  2. we use default arguments to provide start and end
  3. as optional parameters

def find(s, substr, start=0, end=None) :

    if end is None :
        end = len(s)
    for index in range(start, end) :
        if s[index:index+len(substr)] == substr:
            return index
    return -1
  1. Exercise: write a function that behaves like the
  2. range function, except that instead of returning a
  3. list it prints the numbers in the range.
  4. the string module contains useful functions for manipulating strings.
  5. among other things, it includes our find function!

import string help(string.find) print string.find.__doc__

  1. let's write a function that determines if a character is lower case.
  2. here are three ways of doing so:

def is_lower(ch):

    return string.find(string.lowercase, ch) != -1
  1. notice the use of string.lowercase
  2. using the in operator:

def is_lower(ch):

    return ch in string.lowercase
  1. using the comparison operator:

def is_lower(ch):

    return 'a' <= ch <= 'z'

def is_lowercase(s) :

    """determine if a string is all lowercase characters"""
    for c in s :
        if not(c in string.lowercase) :
            return False
    return True
  1. Another constant defined in the string module may surprise you when you print it:

print string.whitespace

  1. Whitespace characters move the cursor without printing anything.

(:sourceend:)