03.09 · Python basics / Watch · 18 min
Use and keep a string method’s result
Python can create a new version of your text. Follow the result so you know which value you are printing.
Activity 9 of 19 in Python basics
Python Basics / 03.09
Understand string methods
Not started
Change case, find text and replace words while keeping track of the original string and each returned result.
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.

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.
Open video & code editor (opens in a new tab)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
YOUR TURN
Save the string returned by upper
Keep original assigned to "Code Zone". Change the assignment to display so it stores the string returned by original.upper(). Keep the two print calls. To pass: original must still hold "Code Zone", display must hold "CODE ZONE", and the program must print CODE ZONE followed by Code Zone, once each. After passing, try changing the original text and run again to see the method work with another value.
CODE ZONE Code Zone
Keep the starting inputs. Print these lines in this order, without extra output.
- original still holds Code Zone
- display holds CODE ZONE
Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.
Ready to checkRun the checks to see your results here.
Your check results
Need a nudge?
Follow the return value
original.upper() returns a string. Where can you put that call so its result becomes the value assigned to display?
Keep the original separate
Do not reassign original to its uppercase version. Assign the result to display, then compare what the two print calls read.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
Strings are immutable. upper returns uppercase text without changing original. The assignment display = original.upper() keeps that returned string. A standalone call discards its return value here. The checks confirm output and stored values; they do not prove which syntax you chose. Try another original value to check that your code transforms it rather than hardcoding the answer.
original = "Code Zone"
display = original.upper()
print(display)
print(original)Ready to try it?
Practice in your browser and see your results.
Progress details
Have a question?
Ask the Code Zone community.
Ask a question in The Code Zone (opens in a new tab)