Space Python Loading Python…

Strings don't change

Fix the codeEasy 3 stages

The inspection tool marks rejected crates by changing the first letter of their code from P to X. It looks like the simplest line in the whole program, and it crashes every time.

Predict what happens, find out why strings can't be changed in place, fix it by building a new string, then change a letter in the middle of a code.

Don't run the code yet. Read it and decide what it will print. Write your guess in prediction, at the top, between the quotes. Then run it and compare.

Any guess passes this stage: the point is to think before you run. After you check, you'll see your prediction next to what Python printed.

This program crashes, and a program that crashes can't be checked. After you run it and see what happens, put a # in front of the line crate[0] = "X", then check: you'll see your prediction next to what the original line did.

Predict, then run

Finish stage 1 to unlock this stage.

Investigate. Store what crate[1:] gives, as text, in rest. Then put crate.lower() on a line of its own, print crate, and store whether it is still "P0042", True or False, in unchanged. Remove the lines you added before you check.

Modify. Instead of changing the string, build a new one: an "X" followed by everything after the first letter, and store it back in crate. It must print:

Crate code: X0042
Investigate slices and methods
Build a new string

Finish stage 2 to unlock this stage.

Duty roster codes are the subsystem, the shift letter and the day: ENG-A-07. The engineers move to shift B. Build a new code with B in place of the shift letter, at index 4, and print both:

Old code: ENG-A-07
New code: ENG-B-07

Take everything before index 4, then "B", then everything after index 4. Don't use replace("A", "B"): the letter could also appear in the subsystem, as in NAV-A-12.

Change the shift letter
Changes only the shift letter
You're not logged in, so your progress is kept in this browser only. Log in to save it, and to use hints and solutions.

Hints

String Indexing and Slicing

string-indexing-slicing.py
# String indexing - every character has a position, counting from 0
mission = "ARTEMIS"
#          0123456

# Grabbing individual characters
print("First letter:", mission[0])    # A
print("Last letter:", mission[-1])     # S

# Slicing [start:stop] - stop is NOT included
print("First three:", mission[0:3])    # ART
print("From index 4 on:", mission[4:])  # MIS
print("Last three:", mission[-3:])      # MIS

# Step slicing [start:stop:step]
launch_code = "A1B2C3D4"
print("Every 2nd:", launch_code[::2])   # ABCD
print("The digits:", launch_code[1::2]) # 1234

# Reversing with a negative step
print("Reversed:", mission[::-1])       # SIMETRA
Open the full example
script.py
PythonLn 1, Col 1Spaces: 4
Loading the Python environment…

Separate arguments with commas. They're passed on every Run as sys.argv[1:].