Section 7.9 String methods
Strings are an example of Python
objects. An object contains both data (the actual string itself) and
methods, which are effectively functions that are built into the object and are available to any
instance of the object.
Python has a function called
dir which lists all the methods available for an object. The
type function returns the type of an object.
>>> help(str.capitalize)
Help on method_descriptor:
capitalize(...)
S.capitalize() -> str
Return a capitalized version of S, i.e. make the first character
have upper case and the rest lower case.
>>>
Activity 7.9.1.
11-9-2: What function lists the methods available for an object?
-
dir()
-
Correct! dir() will list all the methods that can be used with an object.
-
print()
-
Incorrect! print() prints whatever is within the parentheses. Try again.
-
type()
-
Incorrect! type() returns the type of the object within its parentheses. Try again.
-
object()
-
Incorrect! object() returns a new featureless object. Try again.
Calling a
method is similar to calling a function (it takes arguments and returns a value), but the syntax is different. We call a method by typing the variable name, adding a period, and then adding the method call.
For example, the method
upper takes a string and returns a new string with all uppercase letters. Instead of the function syntax,
upper(word), it uses the method syntax,
word.upper().
This form of dot notation specifies the name of the method,
upper, and the name of the string to apply the method to,
word. The empty parentheses indicate that this method takes no argument.
A method call is called an
invocation; in this case, we would say that we are invoking
upper on the
word.
For example, there is a string method named
find that searches for the position of one string within another:
In this example, we invoke
find on
word and pass the letter we are looking for as a parameter.
The
find method can find substrings as well as characters:
It can also take as a second argument the index where it should start looking:
One common task is to remove white space (spaces, tabs, or newlines) from the beginning and end of a string using the
strip method:
Some methods, such as
startswith, return boolean values:
You will note that
startswith requires case to match, so sometimes weβll convert a line to lowercase using the
lower method before we do any checking:
In the last example, the method
lower is called. Then, we use
startswith to see if the resulting lowercase string starts with the letter βhβ. As long as we are careful with the order, we can make multiple method calls in a single expression.
Activity 7.9.2.
Fix the following function. It should use the string method
count to count the number of times a double s (ss) appears in a word. There are 3 mistakes to fix.
Activity 7.9.3.
11-9-9: What is printed by the following statements?
s = "let's go blue!"
print(s.count("e") + s.count("b"))
-
0
-
Incorrect! Both βeβ and βbβ are present in the string. Try again.
-
1
-
Incorrect! There is only one βbβ, but there are multiple eβs as well. Try again.
-
2
-
Incorrect! There are two eβs, but there is a βbβ as well. Try again.
-
3
-
Correct! There are two eβs and one b, so added together, this will print "3".
Activity 7.9.4.
You have attempted
of
activities on this page.