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
Interactive lab

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

  1. Each method call reads the string it is called on from left to right (strip also trims from the right end).
  2. 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.
  3. 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.
  4. 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.
  5. 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.

Educational simulation

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

wordbananaupper()method operationnew stringuppercase lettersnew_wordstored resultreceives stringreturnsassignment
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

Definition

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.

textobject.connects object and methodlowermethod name()invocationfollowed byconnects tocalled with
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

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

Worked example

Identifying the Parts

Read the expressions text.lower(), text.strip(), and text.replace(old, new) as method invocations.

  1. 1
    Find the object

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

  2. 2
    Find the method

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

  3. 3
    Check the parentheses

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

  4. 4
    Track the result

    Each method call produces a result. If that result is needed later, assign it to a name.

Result

All three expressions use object.method_name() syntax. Their shared structure is more important here than memorizing the names separately.

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

Mistakes in Method Calls

  • Common mistake

    Forgetting the dot

    What it looks like
    text lower()
    Why it fails

    The dot is what connects the object to its method.

    Do this instead
    Use object.method_name(), such as text.lower().
  • Common mistake

    Omitting the parentheses

    What it looks like

    word.upper

    Why it fails

    Without parentheses, the method is referenced rather than invoked.

    Do this instead

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

  • Common mistake

    Failing to assign the result

    What it looks like
    word.upper()
    Why it fails

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

    Do this instead

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

  • Common mistake

    Using the wrong method name or arguments

    What it looks like

    text.unknown() or text.replace() when values are expected inside the parentheses

    Why it fails

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

    Do this instead

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

  1. 1
    Check that a string object appears before the dot.
  2. 2
    Check that the dot is present between the object and method name.
  3. 3
    Check that the method name is spelled as intended.
  4. 4
    Check that parentheses are present for an invocation.
  5. 5
    Check whether the returned result was assigned when it must be used later.
  6. 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. 1
    String method calls use object.method_name() syntax.
  2. 2
    The dot connects the string object to its method.
  3. 3
    Parentheses signal that the method is being invoked.
  4. 4
    String methods return new strings rather than modifying the original string.
  5. 5
    Assign the returned value when you need to use it later.
  6. 6
    When 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(), 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.

Related concepts

More concepts starting with S