String Methods: lower, strip, and replace
Method invocation uses dot notation: object.method_name() instead of function syntax like function_name(object).
Try it: String methods, character by character
How Python's str methods (split, strip, replace, find, count, startswith, join, in …) scan a string character by character — and that each call returns a new value while the original string never changes.
How it works
- Each method call reads the string it is called on from left to right (strip also trims from the right end).
- split() with no separator skips runs of whitespace and never returns empty strings; split(sep) cuts at every occurrence of sep and can return empty pieces; maxsplit limits the number of cuts.
- find, index, count, replace and `in` slide a window along the string; occurrences never overlap, find gives -1 and index raises ValueError when nothing matches.
- strip(chars) removes any of the given characters (a set, not a substring) from the ends until it meets one that is not in the set.
- Strings are immutable: every call returns a new str, list, int or bool, and the next call in the chain works on that new value.
Default run (62 steps): s = ' Apples, pears,,figs '. Evaluate ' | '.join(s.strip().lower().split(',')) one method call at a time, left to right. … ' | '.join(s.strip().lower().split(',')) → 'apples | pears | | figs'. And s is still ' Apples, pears,,figs '.
Simplified: Printable ASCII text up to 30 characters and up to four chained calls with short arguments. CPython searches with a faster algorithm (fastsearch) than the window-by-window scan shown, but the results are the same.
Loading the simulation…
A String Enters a Method
A string method call has a clear flow: a string is the object, the dot connects that object to a method, the parentheses invoke the method, and the method returns a result. The same method-invocation structure applies when the method name is lower, strip, or replace.
What do you think happens?
What does the expression word.upper() do when word contains 'banana'?
Reveal answer
It produces a new string with the letters converted to uppercase.
The upper method reads the original string and returns a new string. It does not modify the original string.
Following the Returned Value
Consider the source example new_word = word.upper(). The value stored in word flows into upper. The method performs its operation and returns a new string. Assignment then stores that returned string in new_word. The original word remains unchanged.
After the call, word still contains 'banana'. new_word contains the new uppercase string.Reading Dot Notation
Method invocation means calling a method with dot notation. Its general structure is object.method_name(). The dot connects the object to its method, and the parentheses signal that the method is being invoked.
| Form | Structure | Meaning |
|---|---|---|
| Method invocation | object.method_name() | Calls a method attached to an object |
| Function-call comparison | function_name(object) | Uses function syntax rather than dot notation |
Method invocation uses dot notation instead of the function syntax shown for comparison.
The same structure can be read in the three topic methods: text.lower(), text.strip(), and text.replace(old, new). In each expression, text is the object, the dot connects text to the method name, and the parentheses invoke the method. The replace form also shows values written inside the parentheses.
Three Calls to Trace
Identifying the Parts
Read the expressions text.lower(), text.strip(), and text.replace(old, new) as method invocations.
- 1Find the object
In all three expressions, text appears before the dot and is the object on which the method is called.
- 2Find the method
The method names are lower, strip, and replace. Each name appears after the dot.
- 3Check the parentheses
The parentheses indicate invocation. The replace expression also contains old and new inside the parentheses.
- 4Track the result
Each method call produces a result. If that result is needed later, assign it to a name.
All three expressions use object.method_name() syntax. Their shared structure is more important here than memorizing the names separately.
Mistakes in Method Calls
Common mistake Forgetting the dot
What it looks liketext lower()Why it failsThe dot is what connects the object to its method.
Do this insteadUse object.method_name(), such as text.lower().Common mistake Omitting the parentheses
What it looks likeword.upper
Why it failsWithout parentheses, the method is referenced rather than invoked.
Do this insteadAdd parentheses when you want to call the method: word.upper().
Common mistake Failing to assign the result
What it looks likeword.upper()Why it failsThe method returns a new string, while the original string remains unchanged.
Do this insteadAssign the result when you need it later: new_word = word.upper().
Common mistake Using the wrong method name or arguments
What it looks liketext.unknown() or text.replace() when values are expected inside the parentheses
Why it failsA method call must use the intended method name and the appropriate argument structure.
Do this insteadCheck the method spelling and compare the parentheses with the intended call pattern.
- 1Check that a string object appears before the dot.
- 2Check that the dot is present between the object and method name.
- 3Check that the method name is spelled as intended.
- 4Check that parentheses are present for an invocation.
- 5Check whether the returned result was assigned when it must be used later.
- 6For calls with values inside the parentheses, check that the argument structure matches the intended method call.
Practice the Trace
For each expression, identify the object, method name, parentheses, and any values inside the parentheses: text.lower(), text.strip(), and text.replace(old, new). Then explain where the returned result would go if you assigned the call to a variable.
Hints
- Start at the left side of the dot to find the object.
- The method name appears after the dot.
- Parentheses indicate that the method is being invoked.
- A returned string can be assigned to a variable if you need to use it later.
Debug this expression by naming the missing part: word.upper. Is the method being invoked? Rewrite the expression so that the method is called.
Hints
- The method name is present.
- Look at the characters immediately after the method name.
- Parentheses trigger invocation.
Key Takeaways
- 1String method calls use
object.method_name()syntax. - 2The dot connects the string object to its method.
- 3Parentheses signal that the method is being invoked.
- 4String methods return new strings rather than modifying the original string.
- 5Assign the returned value when you need to use it later.
- 6When debugging, check the dot, method name, parentheses, arguments, and assignment.
At a glance
- Use dot notation to invoke a method on a string.
- Read
text.lower(),text.strip(), andtext.replace(old, new)by separating the object, dot, method name, and parentheses. - A method receives the original string and returns a new string.
- The original string is not modified by the method operation described in the source example.
- Assign the result and systematically check syntax when a method call does not work.