Working with Strings: Indexing, Traversal, and Manipulation
Method invocation uses dot notation: object.method_name() instead of function syntax like function_name(object).
A Method Call in Motion
When Python code calls a method on a string, three connected events matter: the string is identified, the method is invoked, and the method produces a result. The expression word.upper() illustrates this process. The dot connects the string object to its method, while the parentheses signal that the method is being called.
Reading the Syntax
The general method-invocation pattern is object.method_name(). The object is the value the method operates on. The dot operator connects that object to the method. The method name identifies the operation, and the parentheses invoke, or run, the method. This differs from a standalone function call such as function_name(object), where the object appears inside the function's parentheses rather than before a dot.
| Part | Role |
|---|---|
| object | The string the method operates on |
| . | Connects the object to its method |
| method_name | Identifies the operation |
| () | Signals that the method is being invoked |
The four visible parts of object.method_name().
Tracing the Returned String
Consider the assignment new_word = word.upper(). The name word identifies the original string. The dot selects upper as the method connected to that string. The parentheses invoke the method. The method reads the original string, converts its letters to uppercase, and returns a new string. The assignment stores that returned string under new_word.
word contains 'banana'.
new_word contains 'BANANA'.Predict Before Running
What do you think happens?
After these statements, what does each name contain? word = 'banana' new_word = word.upper()
Reveal answer
Answer: word contains 'banana' and new_word contains 'BANANA'.
The upper method reads word and returns a new uppercase string. The original string is not modified, and the returned string is captured by the assignment to new_word.
Following a Method Call
Trace new_word = word.upper() when word contains 'banana'.
Identify the object: word is the string object on which the method operates.
Read the connection: The dot connects word to the upper method.
Read the invocation: The parentheses show that upper is being called.
Track the return value: upper produces a new string with all letters converted to uppercase.
Track the assignment: The returned string is assigned to new_word.
word still contains 'banana', while new_word contains 'BANANA'.
Mistakes in Method Syntax
Forgetting the dot between the object and the method
The dot is what connects the string object to its method.
Fix:
Use word.upper().Omitting the parentheses
Without parentheses, the method is only being referenced; it is not being invoked.
Fix:
Use word.upper() when the goal is to call the method.Failing to assign the returned string
The method returns a new uppercase string, while the original string remains unchanged. Without assigning the result, the uppercase version is not captured for later use.
Fix:
Assign the result when you need to use it later, as in new_word = word.upper().
- First check the object: identify the string that should receive the method call.
- Next check the connection: verify that a dot appears between the object and method name.
- Then check invocation: verify that parentheses appear after the method name when the method should run.
- Finally check the result: decide whether the returned string needs to be assigned to a name.
Practice with the Pattern
Explain the role of each part in new_word = word.upper(): the object, the dot, the method name, the parentheses, and the assignment.
Hints
- Start with the name before the dot.
- The dot connects the object to the method.
- The parentheses indicate invocation.
- The assignment captures the returned string.
A learner writes word.upper and expects the uppercase string to be available under a new name. Identify the syntax issue and rewrite the statement so the result is assigned.
Hints
- Check whether the method has been invoked.
- Check whether the returned string has been assigned.
Key Takeaways
- A method invocation uses object.method_name().
- The dot connects an object to its method, and parentheses invoke the method.
- A string method such as upper reads the original string and returns a new uppercase string.
- The original string remains unchanged, so assign the returned value when you need to use it later.
- When debugging, check the object, dot, method name, parentheses, and assignment in that order.
Key Takeaways
- Method invocation connects a string object to a method with dot notation.
- The parentheses distinguish invoking a method from merely referring to it.
- String methods such as upper return new strings and do not modify the original string.
- Assign the returned value when you want to preserve and use the method's result.
- Systematic debugging starts by checking the object, dot, method, parentheses, and assignment.