String Basics and Indexing
Strings are objects belonging to the str class and possess methods for checking, searching, and transforming text.
A String Is More Than Text
A string is a sequence of text, but in Python it is also an object belonging to the str class. Because it is an object, a string has methods: operations that can check, search, or transform text. This means that working with a string involves both the characters it contains and the operations available through str.
When you see a string method such as startswith(), find(), or join(), you are using behavior provided by the str class.
Character Positions
Indexing lets you refer to characters by position. Positive indices count from the beginning of the string, starting at zero. Negative indices count from the end, with the final character at position negative one. Indexing is useful when a task concerns a particular character rather than the entire string.
Finding a Character by Position
Use the string Python to identify its first character and its final character.
First character: The first character is reached with positive index 0.
Final character: The final character is reached with negative index -1.
Check both directions: Positive indexing starts at the beginning, while negative indexing starts at the end.
For Python, index 0 identifies P and index -1 identifies n.
Three Useful String Methods
String methods let you express common text-manipulation tasks directly. startswith() checks whether a string begins with specified characters. find() locates a substring within a string. join() combines a sequence of strings using a delimiter. These methods illustrate three different kinds of work: checking, searching, and combining.
Choosing the Right Method
Select a string method for each task: check whether a title begins with a prefix, locate a word inside a sentence, or combine several words with a separator.
Check a beginning: Use startswith() when the question is whether text begins with particular characters.
Locate text: Use find() when the task is to locate a substring.
Combine values: Use join() when a sequence of strings must be combined with a delimiter.
The method should match the operation: startswith() checks, find() searches, and join() combines.
Why the Original Stays the Same
String methods return new values rather than modifying the original string. This behavior follows from string immutability: once a string exists, a method does not change that original string's contents. When a method appears to transform text, think of the result as a new value that can be used separately.
Discovering More with help(str)
The three methods in this lesson are only part of the behavior available for str objects. Python's built-in help() function can display the documentation for the str class. Running help(str) reveals the available methods and explains what each method does, which arguments it accepts, and what it returns.
- Identify the text task you need to solve.
- Use help(str) to inspect the methods available for string objects.
- Read the method descriptions, including accepted arguments and returned values.
- Choose a method whose documented behavior matches the task.
- Use the returned value while remembering that the original string remains unchanged.
Documentation is part of the programming process. You do not need to memorize every string method when help(str) can show the complete range and explain how each method behaves.
Mistakes with String Operations
Treating startswith() as a general substring search
startswith() tests the beginning of a string; it is not the method described for locating a substring.
Fix:
Use startswith() for a prefix check and find() when the task is to locate a substring.Assuming find() combines several strings
find() locates a substring, while join() combines a sequence of strings.
Fix:
Choose join() for combining strings with a delimiter.Expecting a string method to modify the original string
String methods return new values because strings are immutable.
Fix:
Work with the returned value and treat the original string as unchanged.Guessing a method instead of checking its documentation
The str class contains many methods, and their documentation explains their behavior, arguments, and return values.
Fix:
Use help(str) to discover suitable methods and verify how they work.
Try the Method Match
For each task, choose between startswith(), find(), and join(): determine whether a product code begins with a given prefix; locate a particular word in a sentence; combine a list of words with a chosen delimiter.
Hints
- Ask whether the task checks a beginning, searches for a location, or combines values.
- Remember that join() uses a delimiter to combine a sequence of strings.
- If you cannot remember another string method, consult help(str).
What do you think happens?
A string method appears to produce transformed text. Does that operation modify the original string or return a new value?
Reveal answer
Answer: It returns a new value and leaves the original unchanged.
Strings are immutable, so string methods do not modify the original string.
Key Takeaways
- Strings are objects belonging to the str class and provide methods for checking, searching, and transforming text.
- Positive string indices count from the beginning, while negative indices count from the end.
- startswith() checks a prefix, find() locates a substring, and join() combines strings with a delimiter.
- String methods return new values because strings are immutable; they do not modify the original string.
- Use help(str) to discover available string methods and read their arguments, behavior, and return values.