Overview of String Processing in Python-2
To be successful you should follow these tasks weekly
read the overview page in each assigned module
read the lecture notes posted in modules
participate in the discussion forums
review the supplement documents and example code posted in the modules
attempt the practice exercise in the module
complete the module assignments
complete the module quiz
Important Reminder
The correct process for responding to discussions is to first post your own reply to the discussion topic. Other participant’s replies will not be visible until you post your first individual reply. Second, you must post a brief reply to at least two other students’ replies. This along with a relevant individual reply will give you full credit in discussion assignments.
Basic String Operations
Python provides several ways to access the individual characters in a string. Strings also have methods that allow you to perform operations on them.
Many of the programs that you have written so far have worked with strings, but only in a limited way. The operations that you have performed with strings so far have primarily involved only input and output. For example, you have read strings as input from the keyboard and from files and sent strings as output to the screen and to files.
There are many types of programs that not only read strings as input and write strings as output, but also perform operations on strings. Word processing programs, for example, manipulate large amounts of text and thus work extensively with strings. Email programs and search engines are other examples of programs that perform operations on strings. Python provides a wide variety of tools and programming techniques that you can use to examine and manipulate strings. In fact, strings are a type of sequence, so many of the concepts that you learned about sequences in our section on looping and collections apply to strings as well.
Accessing the Individual Characters in a String
Some programming tasks require that you access the individual characters in a string. For example, you are probably familiar with websites that require you to set up a password. For security reasons, many sites require that your password have at least one uppercase letter, at least one lowercase letter, and at least one digit. When you set up your password, a program examines each character to ensure that the password meets these qualifications.
we will look at two techniques that you can use in Python to access the individual characters in a string: using the for loop, and indexing.
Iterating over a String with the for Loop
In Python, one of the easiest ways to access the individual characters in a string is to use the for loop.
Here is the general design pattern for processing characters in a String:
for variable in string:
statement
statement
statement
In the general format, variable is the name of a variable and string is either a string literal or a variable that references a string. Each time the loop iterates, variable will reference a copy of a character in string, beginning with the first character. The professional terminology for this is that the loop iterates over the characters in the string
name = ‘Nelson’
for ch in name:
print(ch)
The name variable references a string with six characters, so this loop will iterate six times. The first time the loop iterates, the ch variable will reference 'N', the second time the loop iterates the ch variable will reference 'e'. This will continue until the process gets to the end of the string
Indexing
Another way that you can access the individual characters in a string is with an index. Each character in a string has an index that specifies its position in the string. Indexing starts at 0, so the index of the first character is 0, the index of the second character is 1, and so forth. The index of the last character in a string is 1 less than the number of characters in the string.
The len function can also be used to get the length of a string. The following code demonstrates:
city = Philadelphia
size = len(city)
The second statement calls the len function, passing the city variable as an argument. The function returns the value 11, which is the length of the string ‘Philadelphia’. This value is assigned to the size variable.
The len function is especially useful to prevent loops from iterating beyond the end of a string, as shown here:
city = ‘Philadelphia’
index = 0
while index < len(city):
print(city[index])
index += 1
Notice that the loop iterates as long as index is less than the length of the string. This is because the index of the last character in a string is always 1 less than the length of the string.
String Slicing
You can use slicing expressions to select a range of characters from a string You learned in the lecture on collections that a slice is a span of items that are taken from a sequence of items. This is also referred to as selecting a sub string from the string. A sub string is similar to a sub set of a larger set
To get a slice of a string, you write an expression in the following general format:
string[start : end]
This is similar to how elements were sliced in lists. Just as with slicing a list start is the index of the first character in the slice, and end is the index marking the end of the slice. The expression will return a string containing a copy of the characters from start up to (but not including) end.
Here is an Example:
full_name = 'Patty Ann Nelson' Summary of String Operations
middle_name = full_name[6:9]
The second statement assigns the string 'Lynn' to the middle_name variable. If you leave out the start index in a slicing expression, Python uses 0 as the starting index.
Just as with slicing lists, invalid indexes do not cause slicing expressions to raise an exception.
Once again the following rules apply.
If the end index specifies a position beyond the end of the string, Python will use the length of the string instead.
- If the start index specifies a position before the beginning of the string, Python will use 0 instead.
- If the start index is greater than the end index, the slicing expression will return an empty string.
Testing, Searching, and Manipulating Strings
Python provides operators and methods for testing strings, searching the contents of strings, and getting modified copies of strings
Testing Strings with in and not in
In Python you can use the in operator to determine whether one string is contained in another string. Here is the general format of an expression using the in operator with two strings: string1 in string2
string1 and string2 can be either string literals or variables referencing strings. The expression returns true if string1 is found in string2.
String Methods
Recall from the in class discussions that a method is a function that belongs to an object and performs some operation on that object. Strings in Python have numerous methods to assist programmers.
The chapter covers some of the following types of operations on strings
- Testing the values of strings
- Performing various modifications
- Searching for substrings and replacing sequences of characters
Here is the General Format of a String Method Call:
stringVariable.method(arguments)
stringVariable is a variable that references a string, method is the name of the method that is being called, and arguments is one or more arguments being passed to the method.
There are a variety of method that can determine if a string or character is numeric, if it contains spaces, if a character is upper or lower case and that can search for a specific character or sequence of characters in a larger string. Examine these methods in Chapter 8
Homework
Readings
Discussions for the Week
None
Weekly Quiz