Concepts:Chapter 8
|
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 |