Common String Methods
The data distinguishes one string from another, but the methods are the same toolkit available to every string object. The same method can therefore work with different string data and produce results tailored to the string that calls it.
A String Is More Than Characters
When you create a string in Python, you create an object rather than merely storing a sequence of characters. That object bundles the characters with built-in functions called methods. The characters are the object's data, while the methods form a toolkit for working with that data.
The data distinguishes one string from another, but the methods are the same toolkit available to every string object. The same method can therefore work with different string data and produce results tailored to the string that calls it.
Following a Method Call
Calling upper on a greeting
Suppose a string object contains the characters in the word greeting. What happens when that object calls its upper method?
- 1Start with the object
The string object contains the characters in greeting as its data.
- 2Access the method
Dot notation connects the string object to its upper method.
- 3Call the method
The parentheses indicate that upper is being executed rather than merely referenced.
- 4Produce a result
The method processes the calling string's characters and returns a new uppercase result.
The original string remains unchanged, and the method call produces a new result containing the uppercase form of the string.
This sequence shows the relationship between an object's data and its method. The upper method does not operate on an unrelated string. It operates on the characters stored in the particular string object that calls it.
Reading Dot Notation
Dot notation is the pattern object.method(). It connects an object to a method that belongs to that object.
In a call such as greeting.upper(), greeting identifies the object, the dot tells Python to look for something belonging to that object, and upper identifies the method. The parentheses are essential because they show that the method is being called. Without the parentheses, you are referring to the method itself rather than running it.
| Kind of operation | How it is called | What the syntax communicates |
|---|---|---|
| Standalone function | Called by its own name | The operation is not written as belonging to a particular object |
| String method | Called with object.method() | The operation belongs to the string object and works with that object's data |
The key distinction is how the operation is connected to the data it works with.
Immutable String Results
A string method processes the calling string's data and returns a new result. It does not change the original string. Python strings are immutable, which means the original string's characters remain unchanged after a method call.
When you see a string method call, ask three questions in order: Which string object is calling the method? Which method is being called? What new result does that method return?
Mistakes with Methods
Common mistake Treating a string as only a sequence of characters
What it looks likeThinking of a string only as stored text and overlooking the methods bundled with the string object.
Why it failsA Python string object contains both character data and built-in methods.
Do this insteadThink of the string as an object that packages its data with operations designed for that data.
Common mistake Leaving out the dot
What it looks likeTrying to call a string method without connecting the string object and method name with dot notation.
Why it failsThe dot is what signals that the method belongs to the specific object.
Do this insteadUse the object.method() pattern.Common mistake Leaving out the parentheses
What it looks likeWriting a method name after the dot but not calling it.
Why it failsWithout parentheses, the method is being referred to rather than executed.
Do this insteadAdd parentheses when you want to call the method.
Common mistake Expecting the original string to change
What it looks likeAssuming that calling upper changes the original string's characters.
Why it failsString methods return a new result, and strings are immutable.
Do this insteadTreat the returned result as separate from the unchanged original string.
Check Your Reasoning
A string object contains the characters in greeting. Explain what each part of greeting.upper() represents, what the parentheses signal, and why the original string remains unchanged after the call.
Hints
- Identify the object before the dot.
- Identify the method after the dot.
- Remember that parentheses indicate a call.
- Separate the returned result from the original string.
- 1A strong answer identifies greeting as the string object, upper as the method, and the parentheses as the signal to execute it. The method works with greeting's character data and returns a new uppercase result, while the original string stays unchanged.
Key Takeaways
- A Python string is an object that bundles character data with built-in methods.
- Methods are functions that belong to an object and are called with dot notation.
- Every string object has the same set of methods, but each method operates on the data of the particular string that calls it.
- Parentheses execute a method; without them, the method is only being referenced.
- String methods return new results and do not change the original string because strings are immutable.