String Immutability
String methods are built-in functions that transform, search, and analyze text. Each method behaves differently and returns a different type of result.
A Changed Result
Suppose a string contains unwanted spaces around its text, or a word appears several times and needs to be changed. Python provides string methods for these jobs. The important question is not only what the method does, but also what happens to the original string. Methods such as strip() and replace() return a new string. They do not modify the original string.
What do you think happens?
What will happen to the original string after strip() is called?
Reveal answer
Answer: A new string is returned, while the original remains unchanged.
String methods do not modify the original string. If the transformed result is needed later, it must be stored.
Tracing the Returned String
The diagram shows two results that must be kept separate. The original string still contains its surrounding spaces. The method produces another string without those spaces. Calling the method alone does not replace the original value; storing the returned value is how you keep the transformed text for later use.
text is still " hello "
clean_text is "hello"What String Methods Return
String methods are built-in functions for transforming, searching, and analyzing text. They do not all behave the same way or return the same type of result. A method can return a new string, a boolean value such as True or False, or a number. Reading the return-value description in the documentation helps you decide what to do with the result.
When experimenting with a string method, inspect two things: the value returned by the call and the type of result described by the documentation. This prevents you from assuming that every method changes text or returns another string.
Strip and Replace
| Method | What it acts on | What it returns |
|---|---|---|
| strip() | Whitespace at the beginning and end of a string | A new string without that surrounding whitespace |
| replace() | Occurrences of a specified substring | A new string with the substitutions |
Cleaning and Substituting Text
A text value has unwanted spaces around it, and a repeated word needs to be changed.
Clean the edges: Call strip() on the text. It removes whitespace from the beginning and end, including spaces, tabs, and newline characters, and returns a new string.
Substitute text: Call replace() with the substring to find and the substring to use in its place. The method finds all occurrences and returns another new string.
Keep the results: Assign each returned value to a name if the cleaned or substituted text will be needed later. The original string is not modified by either method.
Use strip() for unwanted whitespace at the edges and replace() for substituting a chosen substring.
raw_text is still " red red "
clean_text is "red red"
updated_text is "blue blue"Reading Method Documentation
Python's official standard library documentation lists the available string methods and explains what each one does. For a method entry, look for three pieces of information: the method name, the parameters it accepts, and what it returns. For replace(), the key parameters are the substring to find and the substring to use as its replacement. For strip(), the essential behavior is removal of whitespace from both ends of the string.
- Identify the method name, such as strip or replace.
- Check what arguments the method accepts.
- Check which part of the text the method affects.
- Check the documented return value and its type.
- Try a small example in a Python interpreter or notebook and compare the result with the documentation.
Common Mistakes
Assuming strip() changes the original string.
The method returns a new string and does not modify the original.
Fix:
Store the result, for example: clean_text = text.strip().Using strip() to replace a word inside the text.
strip() is for whitespace at the beginning and end. It is not the method for substituting a substring.
Fix:
Use replace() when a substring needs to be changed.Using replace() when only surrounding whitespace should be removed.
replace() substitutes matching substrings throughout the text, while strip() specifically removes whitespace from the beginning and end.
Fix:
Use strip() for unwanted edge whitespace.Assuming every string method returns another string.
String methods can return different types, including a new string, a boolean, or a number.
Fix:
Read the method documentation and observe the returned value.
Practice by Prediction
Create a string containing spaces at both ends and call strip() without assigning its result. Then inspect the original string. Next, assign the result of strip() to a new name. Finally, call replace() on the cleaned string to substitute a word that appears more than once. Record the original string, the cleaned string, and the substituted string.
Hints
- Use a string with visible spaces at both edges.
- Compare the original name with the name receiving the return value.
- Choose a substring that occurs more than once so you can observe that all occurrences are replaced.
For each task, choose strip() or replace(), then explain why: remove spaces around user-entered text; change every occurrence of one label to another; search text for a condition; analyze information in a string.
Hints
- strip() is specifically described as removing whitespace from both ends.
- replace() is specifically described as finding and substituting substring occurrences.
- For searching or analyzing, consult the documentation for a method whose return value answers the task.
Key Takeaways
- String methods transform, search, or analyze text, and different methods return different types of results.
- strip() removes whitespace from the beginning and end of a string and returns a new string.
- replace() finds all occurrences of a substring, substitutes another substring, and returns a new string.
- String methods do not modify the original string, so assign the returned value when you need to keep it.
- Use Python's official string-method documentation and small experiments to learn a method's parameters and return value.
Key Takeaways
- String immutability means that string methods return new strings instead of modifying the original string.
- strip() is suited to removing whitespace from the beginning and end of text.
- replace() is suited to substituting every occurrence of one substring with another.
- The method documentation reveals a method's name, parameters, behavior, and return value.
- Experimenting with small strings makes method behavior easier to understand.