thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

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

All courses

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.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

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.

Rob calls upper on hello, stores the returned HELLO in loud, and compares the unchanged original with the new result.
Rob’s comic strip · Python basics

ORIGINAL WALKTHROUGH · 12:03

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

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)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

main.py
main.pypython · example
# Code along with Rob: string methods
# Assign a short text value.
# Compare upper, lower, title and capitalize.
# Keep a returned result and compare it with the original.
# Try find, replace, in and not in with different text.

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.

Save the returned string in a name
string_methods.pypython · example
original = "hello"
loud = original.upper()

print(original)  # hello
print(loud)  # HELLO
Open workspace to paste this example (opens in a new tab)

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

Same input, four different results
string_methods.pypython · example
text = "python PROGRAMMING"
print(text.upper())  # PYTHON PROGRAMMING
print(text.lower())  # python programming
print(text.title())  # Python Programming
print(text.capitalize())  # Python programming
Open workspace to paste this example (opens in a new tab)

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

Position zero versus no match
string_methods.pypython · example
text = "python code"
print(text.find("python"))  # 0
print(text.find("code"))  # 7
print(text.find("Java"))  # -1
Open workspace to paste this example (opens in a new tab)

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

Use the replacement result, keep the original
string_methods.pypython · example
original = "cat and cat"
updated = original.replace("cat", "dog")

print(updated)  # dog and dog
print(original)  # cat and cat
Open workspace to paste this example (opens in a new tab)

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

Presence checks are case-sensitive
string_methods.pypython · example
text = "python code"
print("python" in text)  # True
print("Python" in text)  # False
print("Java" not in text)  # True
Open workspace to paste this example (opens in a new tab)

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

Expected output
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.

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.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

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)

Continue learning

ONE STEP FORWARD

Trace which string each name holds, then fix a method call whose return value was lost.