Concepts / Common String Methods

Common String Methods

Strings are Python objects that bundle both data (the characters) and methods (built-in functions) into a single entity.

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

containsprovidesString objectCharactersdataBuilt-in methodsfunctions
What does a string object contain besides its characters, and how are its data and methods packaged together?

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

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?

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

Access the method: Dot notation connects the string object to its upper method.

Call the method: The parentheses indicate that upper is being executed rather than merely referenced.

Produce a result: The method processes the calling string's characters and returns a new uppercase 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.

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

Reading Dot Notation

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

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.

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

  • Treating a string as only a sequence of characters

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

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

  • Leaving out the dot

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

    Fix: Use the object.method() pattern.

  • Leaving out the parentheses

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

    Fix: Add parentheses when you want to call the method.

  • Expecting the original string to change

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

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