Introduction to Python Objects
Strings are Python objects that bundle both data (the characters) and methods (built-in functions) into a single entity.
A String as a Bundle
When you create a string in Python, you are doing more than storing a sequence of characters. You are creating an object: a single entity that bundles the characters with built-in functions that know how to work with them. These built-in functions are called methods.
The character data distinguishes one string from another. The methods form a toolkit of operations that make sense for strings. This is an example of bundling data together with the operations that work on that data, a core principle of object-oriented programming.
Following a Method Call
A method is a function that belongs to an object. When a method is called on a string, it receives that string's data, processes the data according to its built-in logic, and returns a new result. The method therefore connects a general operation with the particular characters stored in the object that calls it.
Tracing upper on a string
Consider the expression greeting.upper(). What role does each part play?
Find the object: greeting identifies the string object whose character data will be used.
Find the method: upper is the built-in method that converts the string's characters to uppercase.
Call the method: The parentheses show that upper is being executed rather than merely referred to.
Receive the result: The method processes greeting's data and returns a new uppercase result.
The expression asks the string object stored in greeting to run its upper method on its own character data.
Reading Dot Notation
Dot notation is the syntax that connects an object to one of its methods. In greeting.upper(), greeting is the object, the dot tells Python to look for something belonging to that object, and upper() identifies and calls the method.
The parentheses matter. With parentheses, Python calls or executes the method. Without them, you are referring to the method itself rather than running it.
One Toolkit, Many Strings
Every string object has the same set of methods available, but each method operates on the data of the specific string that calls it. The method is shared as part of the string toolkit; the result depends on the individual string's characters.
Suppose two different string objects each call upper(). They use the same built-in operation, but each operation works with the characters in the string that called it. The method does not need to be rewritten for each string because it is already bundled into the string object's available toolkit.
Mistakes with Methods
Treating a method as a separate function with no connection to the object
A method belongs to an object and operates on that object's data.
Fix:
Read the expression from left to right: identify the string object, follow the dot, and then identify the method that acts on that string.Leaving out the dot
The dot tells Python to look for the method as something belonging to the object.
Fix:
Use the object.method() structure when calling a method.Leaving out the parentheses when intending to run the method
Without parentheses, the method is being referred to rather than executed.
Fix:
Include parentheses when the goal is to call the method.Expecting a string method to change the original string
String methods return a new result, and strings are immutable in Python.
Fix:
Treat the method call as producing a new result while the original string remains unchanged.
Check Your Understanding
What do you think happens?
Before reading the explanation, predict what happens conceptually when a string object calls upper(). Does the method use the string's own data, or does it operate independently of the object?
Reveal answer
Answer: It uses the calling string's data.
A method belongs to an object and receives that object's data. The same upper method can be available to every string, while each call produces a result based on the particular string that called it.
Explain the parts of greeting.upper() in your own words. Identify the object, the method, the role of the dot, and the role of the parentheses. Then explain why the original string is not changed by the method call.
Hints
- Start with the name before the dot.
- The dot connects the object to something that belongs to it.
- The parentheses show that the method is being called.
- Remember that string methods return a new result.
Key Takeaways
- A string is a Python object that bundles character data with built-in methods.
- A method is a function that belongs to an object and operates on that object's data.
- Dot notation connects an object to a method, as in greeting.upper().
- Parentheses indicate that the method is being called.
- String methods return new results; they do not change the original string because strings are immutable.
Key Takeaways
- Strings bundle character data and methods into one Python object.
- Methods are object-owned functions that work with the data of the object that calls them.
- Dot notation identifies the object and the method, while parentheses execute the method.
- The same method can be available to many string objects and produce results tailored to each string.
- String methods return new results rather than changing the original string.