CS 1110 - Introduction to Programming

Starting Out with Python
Chapter 8, More about Strings

Objectives:

This lesson covers chapter 8, which teaches us to use strings in new ways. Objectives important to this lesson:

  1. Basic string operation
  2. String slicing
  3. Testing, searching, and manipulating strings
Concepts:
Chapter 8
Basic String Operations

Providing some continuity with the last chapter, the text reminds us that a string is composed of a series of characters, typically text characters. Like the lists in chapter 7, strings are also sequences, and they can be manipulated in similar ways.

In chapter 7, we used the index() method of a list to find the first occurrence of a value in it. In example program 8-1, the author shows us a different approach to finding out how many times a value occurs in a string. He starts by declaring that a variable called count is equal to 0.

count = 0
my_string = input('Enter a sentence: ")
for ch in my_string:
     if ch == 'T' or ch =='t'
        count += 1

The code says to ask the user for a sentence, then save it it my_string. Then a for loop starts, and it will run for every character in the string. In each iteration, if the variable ch (which is equal to the current character in the string) happens to be an upper or lower case T, then increment the value of count. This will give us the total number of Ts in the sentence. Before we go on, can you imagine what you might do if you wanted to keep track of the position of each of those Ts?

As you might guess, you can treat the string like a list in another way: each character in the string has an index. Indices start with 0, just like they do in lists, and continue through len(string_name) -1. As we discussed last week, the program will raise an exception error (an IndexError) if we try to read beyond the length of the actual string. This is why it is a bad idea to run a loop that reads a string  character by character, until it reaches what we hope is the last index. By the way, before you get excited about writing to index positions in a string, the book warns us that strings are like tuples, in that they are immutable sequences. We cannot actually write to an index position in a string. We can, however, work around that.

The text begins a discussion about concatenation of strings, and it shows us several familiar ways to put two or more strings together. First, we can use the + sign as a concatenation operator:

fname = 'Doug'
lname = 'Witten'
full_name = fname + ' ' + lname

Despite the statement that a list is immutable, we could use code like the lines above to get input from a user, assigning new strings to fname, lname and full_name in each iteration of a loop.

Let's write that loop, but make it a little different:

full_name = input("Enter the user\'s first name: ")
full_name += ' '
full_name += input("Enter the user\'s last name: ")

This time we used a += operator, but the process is mostly the same. In both cases, we did not change the actual elements inside a string. Instead, we changed the contents of the variables that held the strings.

String Slicing

The text goes on to talk about slicing strings, which uses the slice notation we saw for lists in chapter 7. I like the author's discussion in this chapter better. Using the example above, full_name would hold the string "Doug Witten". This string is 11 characters long, and has indices 0 through 10. Consider this code:

print(full_name[0:4])
print(full_name[5:])

The first line says to print, starting at index 0 and stopping just before index 4. In other words, print the first four characters of the string. The second line says to print, starting at index 5 and stopping when you reach the end of the string, because no value was given for the stop value.

slice notation
Meaning
[x:y]
start at index x, and stop just before index y
[x: ]
start at index x, and stop at the end of the string
[x:y:z]
start at index x, stop just before index y, and step by the value of z
[-x:]
a negative start value means start at the end of the string, count to the left for the value of x, and that will take you to your real start index. from there, select all characters to the end of the string

The text provides a few more examples, but they are the same ideas that we saw in the last chapter.

Testing, Searching, and Manipulating Strings

As we saw with lists, we can test for the presence of a string inside another string with a conditional statement:

if string1 in string2:

This code would return a True value if string1 exists in string2, and it would return a False value if it does not.

The text provides a list in table 8-1 of several testing methods that are built into every string in Python. Note that for every method in the list, the string must contain at least one character.

Testing Method
Description
isalnum()
True if string only holds alphanumeric characters
isalpha()
True if string only holds alphabetic characters
isdigit()
True if string only holds numeric digits
islower()
True if the alphabetic characters in the string are all lower case
isspace()
True if the string only holds whitespace characters: spaces, newlines, and/or tabs
isupper()
True if the alphabetic characters in the string are all upper case

The text continues with a section on methods that can be used to produce a modified version of the string. Remember that you can't change the actual string, but you can create another version of it.


Modification Method
Description
lower()
Changes all upper case letters to lower case letters in the returned copy.
lstrip()
Removes leading whitespace characters in the returned copy.
lstrip(char)
Removes leading instances of the specified character in the returned copy.
rstrip()
Removes trailing whitespace characters in the returned copy.
rstrip(char)
Removes trailing instances of the specified character in the returned copy.
strip()
Removes leading and trailing whitespace characters in the returned copy.
Strip(char)
Removes leading and trailing instances of the specified character in the returned copy.
upper()
Changes all lower case letters to upper case letters in the returned copy.

The text points out that upper() and lower() are useful when you have to check for variant capitalizations from the user that all trigger the same result. The various strip methods are useful, but they only work until they encounter some other character than the one they are stripping.

The text turns to searching for and replacing strings. Before we start on this, remember once more, strings are immutable, but we can make a change in a new string that we create by pasting together parts of the old string and new elements that represent the change.

The text shows us four methods related to searching and replacing:

Method
Description
endswith(substring)
Returns True if the substring is found at the end of the string whose method you are calling.
find(substring)
If the substring is found, the method returns the lowest index where the substring exists in the string whose method you are calling, else it returns -1.
replace(old, new)
The method creates a new string in which all instances of old are replaced with new, and that is the string that is returned.
startswith(substring)
Returns True if the substring is found at the beginning of the string whose method you are calling.

The text provides some examples using these methods. You should walk through the author's discussion of them, using them on your own computer to become familiar with them.

The text reviews the repetition operator, the *, which can concatenate a string with itself multiple times. This is illustrated in sample program 8-8. The final discussion in the chapter is about splitting a string. Strings in python have a method called split(), which reads the words in the string, and produces a list containing those words. The split() method assumes that words are separated in the string by spaces, unless you pass it a different character as an argument. In that case, split() will look for characters on either side of the specified separator, and will interpret those substrings as words. The text explains that this can be useful when you want to parse out the elements in a date that is written with forward slashes as separators.

It is also useful when you want to turn a string into a series of substrings, which is what that list will be once you use the split() method.

Assignments

Assignments for these chapters will be found in Blackboard. We will explore that in class.