# strings.py # A string is an ordered sequence of characters. # String is a data structure and just like integer is a data structure. # Integer is a very simple data structure whereas string is a more # complex data structure. Every time you encounter a new data structure # in any 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 = "basketball" # # 2. How do you view an element of the data structure? This is what # we call 'read access'. # # Ex. letter = sports[0] # to access the first character # print sports[1] # to access the second character # # 3. How do you change an element in the data structure, if possible? # For example, if we want to modify the contents of sports so that # it holds "Baseball" instead of "baseball", we would have to assign # a new character in that location of the sports string by using an # assignment operator like this: # # sports[0] = 'B' # # 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 'B' in the # first location 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. # Changing the value of a variable is called 'write access', and # there are two forms of write access we can think of when we deal # with a string. # (1) changing a character in the string, e.g., by doing something # like: # # sports[0] = 'B' # # but that is not allowed with a string because string is # immutable. # (2) setting a new string to be the value of the variable, e.g., # # sports = Baseball # # which is allowed in Python. # # 4. How do you destroy the data structure when we are done using it. # In a language such as C or C++ you 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 # Indices in Python are 0-based as in most other programming languages # So, fruit[1] is accessing the second character in the string. # 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): print fruit[i] i = i + 1 # Exercise: Create a function that takes one string argument and prints the # letters backward. # We have learned one kind of a loop in Python so far: 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 # really means. So, the example below can be read as # # 'for each letter referred to as c in the string # fruit, print the character c' # # thus printing each letter in the string to the standard output device, # i.e., the computer screen. # print "printing fruit using a for loop" for c in fruit: print c # Here is a simple yet fun thing to do. I think you can figure out what # this 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] # 0 to 5, but 0 inclusive, 5 exclusive print "s[7:11] = ", s[7:11] print "s[17:21] = ", s[17:21] print print "s[:3] = ", s[:3] # 0 to 3, but 0 inclusive, 3 exclusive print "s[17:] = ", s[17:] # 17 to the end, with 17 inclusive # 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 a banana!" else: print "No, we don't have a banana!" 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 a banana!" # 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') print "find(\"apple\", 'p') = ", find("apple", 'p') # Counting - 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) # Two of those are digits and find: # print print "string.digits = ", string.digits print "string.find = ", string.find 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__" print string.find.__doc__ # Based on the information obtained from the documentation, we can now # call find: # print "string.find(\"banana\", \"nan\") = ", string.find("banana", "nan") # Exercise 1: # # See Section 8.13 of [Downey] to see more exercise problems.