Concepts / String Methods: lower, strip, and replace

String Methods: lower, strip, and replace

Method invocation uses dot notation: object.method_name() instead of function syntax like function_name(object).

  • Programming

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

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.

receives stringreturnsassignmentwordbananaupper()method operationnew stringuppercase lettersnew_wordstored result
What happens to a string as it moves through a method invocation?
python
Output
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.

followed byconnects tocalled withtextobject.connects object and methodlowermethod name()invocation
What does each part of text.lower(), text.strip(), or text.replace(old, new) represent?
FormStructureMeaning
Method invocationobject.method_name()Calls a method attached to an object
Function-call comparisonfunction_name(object)Uses function syntax rather than dot notation

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.

Find the object: In all three expressions, text appears before the dot and is the object on which the method is called.

Find the method: The method names are lower, strip, and replace. Each name appears after the dot.

Check the parentheses: The parentheses indicate invocation. The replace expression also contains old and new inside the parentheses.

Track 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.

inputreturnsoriginal stringwordmethod callword.upper()new stringnew_word
What changes in the data flow when a string method is applied?

Mistakes in Method Calls

  • Forgetting the dot

    The dot is what connects the object to its method.

    Fix: Use object.method_name(), such as text.lower().

  • Omitting the parentheses

    Without parentheses, the method is referenced rather than invoked.

    Fix: Add parentheses when you want to call the method: word.upper().

  • Failing to assign the result

    The method returns a new string, while the original string remains unchanged.

    Fix: Assign the result when you need it later: new_word = word.upper().

  • Using the wrong method name or arguments

    A method call must use the intended method name and the appropriate argument structure.

    Fix: Check the method spelling and compare the parentheses with the intended call pattern.

  1. Check that a string object appears before the dot.
  2. Check that the dot is present between the object and method name.
  3. Check that the method name is spelled as intended.
  4. Check that parentheses are present for an invocation.
  5. Check whether the returned result was assigned when it must be used later.
  6. For calls with values inside the parentheses, check that the argument structure matches the intended method call.

Practice the Trace

EASY

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.
EASY

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

  1. String method calls use object.method_name() syntax.
  2. The dot connects the string object to its method.
  3. Parentheses signal that the method is being invoked.
  4. String methods return new strings rather than modifying the original string.
  5. Assign the returned value when you need to use it later.
  6. When debugging, check the dot, method name, parentheses, arguments, and assignment.

Key Takeaways

  • Use dot notation to invoke a method on a string.
  • Read text.lower(), text.strip(), and text.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.