# strings.py # A string is an ordered set of characters. # A string is a data structure and an integer is a data structure. An # integer is a very simple data structure whereas a string is a more # complex data structure. Every time you encounter a new data structure # in any new language that you learn, figure out four things: # # 1. How do you create an instance of that data structure and give # a name to it? # # Ex. sports = "golf" # # 2. How do you view an element of the data structure? This is what # we call a 'read access'. # # Ex. letter = sports[0] # to access the first letter # print sports[1] # # 3. How do you change an element in the data structure? # For example, if we want to modify the contents of sports so that # it holds "golg" instead of "golf", we might try to assign a new # letter in that location of the sports string by using an assignment # operator like this: # # sports[3] = 'g' # # but modifying an element in place in string is not allowed in # Python. So, we call string an immutable data structure in Python. # The best you can do is to create a new string with a 'g' in that # place and reassign the whole string to be the new value of the # variable sports. When we study lists later, we will try to modify # an element in a list in place. Stay tuned. # # 4. How do you destroy the data structure when we are done using it. # In a language such as C or C++ we would have to worry about that, # but in Python, you don't need to. So, actually there are only # 3 things to worry about when you encounter a new data structure # in Python. Sometimes, only 2 as with strings. # Let's see some strings and related functions that we can use to manipulate # strings. # Define a variable with the string value 'banana" as its initial value fruit = "banana" # String is a compound value and we can access individual pieces in it. # Here we are accessing the second character in fruit. letter = fruit[1] print "letter = ", letter # index in Python is 0-based as in most other programming languages print "fruit[0] = ", fruit[0] # length of a string print "len(fruit) = ", len(fruit) # more looping using strings print "printing fruit using a while loop" i = 0 while i < len(fruit): letter = fruit[i] print letter i = i + 1 # Exercise: Create a function that takes one string argument and prints the # letters backward. # We have leaned one kind of loop in Python: the 'while' loop. Well, there # is another looping construct in Python: the 'for' loop. As you can see # below, it has a very simple intuitive form. This loop is often referred # to as the 'for-each' loop because that is what it does. So, the example # below can be read as 'for each letter referred to as char in the string # fruit, print the char' thus printing each letter in the string to the # standard output, the screen. print "printing fruit using a for loop" for char in fruit: print char # Here is a simple yet fun thing to do. I think you can figure out what it # does. prefixes = "JKLMNOPQ" suffix = "ack" for letter in prefixes: print letter + suffix # slicing a string - by using the ':' operator we can specify a range # within the string. s = "Peter, Paul, and Mary" print "s[0:5] = ", s[0:5] print "s[7:11] = ", s[7:11] print "s[17:21] = ", s[17:21] print print "s[:3] = ", s[:3] print "s[17:] = ", s[17:] # comparing strings - we can use the usual comparison operators that we use # for numbers to compare two strings. word = "apple" if word == "banana": print "Yes, we have bananas!" else: print "No, we have no bananas!" if word < "banana": print "Your word," + word + ", comes before banana." elif word > "banana": print "Your word," + word + ", comes after banana." else: print "Yes, we have bananas!" # finding a letter in a string - we can define a function that does that # like this: def find(str, ch): index = 0 while index < len(str): if str[index] == ch: return index index = index + 1 return -1 print "find(\"apple\", 'l') = ", find("apple", 'l') # counting - or count the occurrences of a letter in a string. def count(str, ch): count = 0 for char in str: if char == ch: count = count + 1 return count print "count(\"apple\", 'p') = ", count("apple", 'p') print print # built-in string module import string # to see what things are included in the module: # print "dir(string) . . . . . . . . " print dir(string) # one of those is digits # print "string.digits = ", string.digits print print # if you want to find the documentation on 'find', try this. Similarly you # can find documentation on other functions as well. # print string.find.__doc__ # based on the information obtained from the documentation, we can now # call find: # print "string.find(\"banana\", \"na\") = ", string.find("banana", "na") # Exercise 1: # # See Section 7.15 of [DEM] if you want to see some exercise problems.