Comp 112

Lecture 5

Strings and Methods

2017.10.03

Strings

String Review

We know that a string is a sequence of characters.

So far we can:

Now, we’ll learn how to do more sophisticated string manipulations.

String Indexing

'hello!' [0]  == 'h'
'hello!' [4]  == 'o'
'hello!' [-2] == 'o'
'hello!' [-5] == 'e'

Index Bounds

In order to be valid index for a string s,

String Processing

Combining looping with indexing to process a string:

def reverse_string (s) :
    """
    signature: str -> str
    reverses a string
    """
    i  =  0             #  index counter
    acc  =  ''          #  result accumulator
    while i < len (s) :
        acc  =  s [i] + acc
        i  +=  1
    return acc

Substrings

'hello!' [1 : 5]   ==  'ello'
'hello!' [4 : 6]   ==  'o!'
'hello!' [0 : 4]   ==  'hell'
'hello!' [-6 : -2] ==  'hell'
'hello!' [3 : 3]   ==  ''
'hello!' [0 : 6]   ==  'hello!'

Slice Shorthands

For convenience, you can omit either index:

'hello!' [  : 3] == 'hel'
'hello!' [3 :  ]  == 'lo!'
'hello!' [  :  ] == 'hello!'

Namespaces and Methods

Namespaces

String Functions

Many useful functions for strings are located in the “str” namespace, including:

str.isdigit (<string>)  #  predicate that returns True if all characters of a nonempty string are numerals
str.isalpha (<string>)  #  predicate that returns True if all characters of a nonempty string are letters
str.islower (<string>)  #  predicate that returns True if the string contains letters, all of which are lowercase
str.isupper (<string>)  #  predicate that returns True if the string contains letters, all of which are uppercase
str.lower (<string>)    #  returns the lowercase version of a string, leaving non-letters unchanged
str.upper (<string>)    #  returns the uppercase version of a string, leaving non-letters unchanged

Methods

Method syntax is more concise but rather restrictive. It works only for functions that:

String Methods

All of the functions from the “str” namespace that we just saw can be written as methods:

'42'.isdigit ()                  #  same as str.isdigit ('24')
'hello!'.isalpha ()              #  same as str.isalpha ('hello!')
'hello!'.islower ()              #  same as str.islower ('hello!')
'??'.isupper ()                  #  same as str.isupper ('??')
'hello!'.upper ()                #  same as str.upper ('hello!')
'i LIKE to SHOUT'.lower ()       #  same as str.lower ('i LIKE to SHOUT')

Additional String Operations

String Comparisons

in Operator

'he' in 'hello!'  #  evaluates to True
'lo' in 'hello!'  #  evaluates to True
'ho' in 'hello!'  #  evaluates to False
'l'  in 'hello!'  #  evaluates to True
''   in 'hello!'  #  evaluates to True

for Loops

Example: Counting Vowels

def is_vowel (char) :
    """
    signature: str -> bool
    predicate for vowels
    """
    return len (char) == 1 and char.lower () in 'aeiou'
def count_vowels (s) :
    """
    signature: str -> int
    returns the number of vowels in a string
    """
    acc  =  0
    for char in s :
        if is_vowel (char) :
            acc  +=  1
    return acc

To Do This Week: