Concepts / Common String Methods

Common String Methods

The data distinguishes one string from another, but the methods are the same toolkit available to every string object. The same method can therefore work with different string data and produce results tailored to the string that calls it.

  • Programming

A String Is More Than Characters

When you create a string in Python, you create an object rather than merely storing a sequence of characters. That object bundles the characters with built-in functions called methods. The characters are the object's data, while the methods form a toolkit for working with that data.

String objectCharactersdataBuilt-in methodsfunctionscontainsprovides
What does a string object contain besides its characters, and how are its data and methods packaged together?
Key point

The data distinguishes one string from another, but the methods are the same toolkit available to every string object. The same method can therefore work with different string data and produce results tailored to the string that calls it.

Following a Method Call

Worked example

Calling upper on a greeting

Suppose a string object contains the characters in the word greeting. What happens when that object calls its upper method?

  1. 1
    Start with the object

    The string object contains the characters in greeting as its data.

  2. 2
    Access the method

    Dot notation connects the string object to its upper method.

  3. 3
    Call the method

    The parentheses indicate that upper is being executed rather than merely referenced.

  4. 4
    Produce a result

    The method processes the calling string's characters and returns a new uppercase result.

Result

The original string remains unchanged, and the method call produces a new result containing the uppercase form of the string.

This sequence shows the relationship between an object's data and its method. The upper method does not operate on an unrelated string. It operates on the characters stored in the particular string object that calls it.

greetingstring dataupper()methodUppercase resultnew stringcallsreturns
How does a string method operate on the characters stored in the particular string object that calls it?

Reading Dot Notation

Definition

Dot notation is the pattern object.method(). It connects an object to a method that belongs to that object.

greetingobject.connects object and methoduppermethod name(starts call)executes call
How does Python use the dot between a string object and a method name to access and call that method?

In a call such as greeting.upper(), greeting identifies the object, the dot tells Python to look for something belonging to that object, and upper identifies the method. The parentheses are essential because they show that the method is being called. Without the parentheses, you are referring to the method itself rather than running it.

Kind of operationHow it is calledWhat the syntax communicates
Standalone functionCalled by its own nameThe operation is not written as belonging to a particular object
String methodCalled with object.method()The operation belongs to the string object and works with that object's data

The key distinction is how the operation is connected to the data it works with.

Immutable String Results

A string method processes the calling string's data and returns a new result. It does not change the original string. Python strings are immutable, which means the original string's characters remain unchanged after a method call.

Best practice

When you see a string method call, ask three questions in order: Which string object is calling the method? Which method is being called? What new result does that method return?

Mistakes with Methods

  • Common mistake

    Treating a string as only a sequence of characters

    What it looks like

    Thinking of a string only as stored text and overlooking the methods bundled with the string object.

    Why it fails

    A Python string object contains both character data and built-in methods.

    Do this instead

    Think of the string as an object that packages its data with operations designed for that data.

  • Common mistake

    Leaving out the dot

    What it looks like

    Trying to call a string method without connecting the string object and method name with dot notation.

    Why it fails

    The dot is what signals that the method belongs to the specific object.

    Do this instead
    Use the object.method() pattern.
  • Common mistake

    Leaving out the parentheses

    What it looks like

    Writing a method name after the dot but not calling it.

    Why it fails

    Without parentheses, the method is being referred to rather than executed.

    Do this instead

    Add parentheses when you want to call the method.

  • Common mistake

    Expecting the original string to change

    What it looks like

    Assuming that calling upper changes the original string's characters.

    Why it fails

    String methods return a new result, and strings are immutable.

    Do this instead

    Treat the returned result as separate from the unchanged original string.

Check Your Reasoning

EASY

A string object contains the characters in greeting. Explain what each part of greeting.upper() represents, what the parentheses signal, and why the original string remains unchanged after the call.

Hints
  • Identify the object before the dot.
  • Identify the method after the dot.
  • Remember that parentheses indicate a call.
  • Separate the returned result from the original string.
  1. 1
    A strong answer identifies greeting as the string object, upper as the method, and the parentheses as the signal to execute it. The method works with greeting's character data and returns a new uppercase result, while the original string stays unchanged.

Key Takeaways

  • A Python string is an object that bundles character data with built-in methods.
  • Methods are functions that belong to an object and are called with dot notation.
  • Every string object has the same set of methods, but each method operates on the data of the particular string that calls it.
  • Parentheses execute a method; without them, the method is only being referenced.
  • String methods return new results and do not change the original string because strings are immutable.

Related concepts

More concepts starting with C