Main.StringExamples History

Hide minor edits - Show changes to markup

February 11, 2010, at 02:52 PM MST by 10.84.44.74 -
Changed lines 105-106 from:
  1. here's a version that searches in strng[start:end]
  2. we use default arguments to
to:
  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
Changed line 109 from:
    if end==None :
to:
    if end is None :
February 10, 2010, at 09:58 AM MST by 10.84.44.94 -
Changed lines 34-35 from:
    print s[i]
to:
    print fruit[i]
Changed lines 48-49 from:
to:
  1. these are called slices
Added lines 56-61:

def extract_every_2nd(s) :

    result = ''  # the empty string
    for i in range(0, len(s), 2) :
        result += s[i]
    return result
February 09, 2010, at 11:32 AM MST by 10.84.44.105 -
Added lines 1-144:

(: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 s[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. some shortcuts:

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

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

print s[:]

  1. It is tempting to use the bracket operator in order to modify
  2. a string.

greeting = "Hello, world!" greeting[0] = 'J' # ERROR!

  1. strings are immutable, i.e. cannot be changed once created.
  2. 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):

    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)

def find(s, substr) :

    for index in range(len(s)) :
        if s[index] == ch:
            return index
    return -1
  1. here's a version that searches in strng[start:end]
  2. we use default arguments to

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

    if end==None :
        end = len(s)
    for index in range(start, end) :
        if s[index] == ch:
            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'
  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:)