Working with Strings: Indexing, Traversal, and Manipulation
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.
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
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'.
- 1Identify the object
word is the string object on which the method operates.
- 2Read the connection
The dot connects word to the upper method.
- 3Read the invocation
The parentheses show that upper is being called.
- 4Track the return value
upper produces a new string with all letters converted to uppercase.
- 5Track the assignment
The returned string is assigned to
new_word.
word still contains 'banana', while new_word contains 'BANANA'.
Mistakes in Method Syntax
Common mistake Forgetting the dot between the object and the method
What it looks likeword upper()Why it failsThe dot is what connects the string object to its method.
Do this insteadUse word.upper().Common mistake Omitting the parentheses
What it looks likeword.upper
Why it failsWithout parentheses, the method is only being referenced; it is not being invoked.
Do this insteadUse word.upper() when the goal is to call the method.
Common mistake Failing to assign the returned string
What it looks likeword.upper()Why it failsThe 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.
Do this insteadAssign the result when you need to use it later, as in new_word = word.upper().
- 1First check the object: identify the string that should receive the method call.
- 2Next check the connection: verify that a dot appears between the object and method name.
- 3Then check invocation: verify that parentheses appear after the method name when the method should run.
- 4Finally check the result: decide whether the returned string needs to be assigned to a name.
word.upper
The method is referenced but not invoked, and no returned string is assigned.
new_word = word.upper()The method is invoked, and its returned string is assigned to new_word.
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
- 1A method invocation uses
object.method_name(). - 2The dot connects an object to its method, and parentheses invoke the method.
- 3A string method such as upper reads the original string and returns a new uppercase string.
- 4The original string remains unchanged, so assign the returned value when you need to use it later.
- 5When debugging, check the object, dot, method name, parentheses, and assignment in that order.
At a glance
- 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.