YOUR SAVED CODE-ALONG
Code along: Python methods
Keep the lesson open on your other screen. Your edits stay in this workspace, separate from the practice activity. Sign in to save and reload a private account draft across devices, or share a snapshot when you need help.
CODE-ALONG / THEATER MODE
Understand string methods
ORIGINAL WALKTHROUGH · 12:03
Watch with Rob.
Pause whenever you want to try a change. You can return to the lesson at any time.
You are signed in, but video access needs attention. Check your account access. Your saved work is kept.
Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.
Video settings & progress
Your lesson progress
Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.
Start the video to enable typing controls.
YOUR TURN
Code along: Python methods
Pause the video to type and run short examples. Compare each case method on the same text. Save a returned string and print the original too. Try find on a match at index zero and on missing text, then compare replace and the in operator. These notes outline the topic; they are not a transcript or a fixed-answer challenge.
Load an example, edit it and run it. Word wrap is enabled.
Your check results
Lesson reference
Examples load into this same code-along only after you confirm replacing your code.
A method call uses a dot and parentheses
A string method is an operation you call on a string. In original.upper(), original provides the text, the dot introduces the method name, and the parentheses call it. upper returns a string with its cased letters converted to uppercase.
The call does not change the original string. Assign its returned value to loud, then print both names to see hello followed by HELLO. Calling original.upper() on its own would discard that result.
original = "hello"
loud = original.upper()
print(original) # hello
print(loud) # HELLOCompare four case methods
upper converts cased letters to uppercase; lower converts them to lowercase. With simple words separated by spaces, title capitalizes each word, while capitalize titlecases the first character and lowercases the rest.
Run the example and compare the last two lines: Python Programming and Python programming. These are mechanical text operations, not a guarantee of correct capitalization for every person’s name, acronym or language. Apostrophes can produce surprising results with title.
text = "python PROGRAMMING"
print(text.upper()) # PYTHON PROGRAMMING
print(text.lower()) # python programming
print(text.title()) # Python Programming
print(text.capitalize()) # Python programmingFind returns a position, including zero
find searches for text and returns the starting index of its first match. If there is no match, it returns -1. The example prints 0, 7 and -1. Zero is a real match at the beginning; it does not mean missing.
Searches are case-sensitive. text.find("Python") does not match the lowercase python in this example. Use the in operator when you only want to know whether a match exists.
text = "python code"
print(text.find("python")) # 0
print(text.find("code")) # 7
print(text.find("Java")) # -1Replace creates a changed version
replace takes the text to search for and the replacement text. By default it replaces every matching occurrence. In this example, both cat words become dog in updated, while original still holds cat and cat.
Match spelling and capitalization exactly. Save the result or pass it directly to print; the method does not edit the original string in place.
original = "cat and cat"
updated = original.replace("cat", "dog")
print(updated) # dog and dog
print(original) # cat and catUse in when the question is yes or no
The in and not in operators check whether text is present. They are operators, not string methods, so they do not use a dot or method parentheses. Their answers print as True or False, Boolean values we will explore later.
This example prints True, False and True. The second result is False because Python has a capital P. In the practice, focus on one common bug: calling a method but printing a value that never received its result.
- Python documentation: string methods (opens in a new tab)
- Python documentation: find (opens in a new tab)
- Python documentation: replace (opens in a new tab)
text = "python code"
print("python" in text) # True
print("Python" in text) # False
print("Java" not in text) # TrueBuild it one step at a time
Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.
The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.
Start mini-lesson(opens in a new tab)4 steps · Saved work · Instant feedback
See the attached lesson’s comic
