Space Python Loading Python…

Sensor ID parts

Write the codeMedium 2 stages

Every sensor on Python One has an ID made of its subsystem and a unit number: ENG-2 is the second engine sensor. The maintenance tool needs both parts separately, and the number as a real number, so it can find the unit next to it.

Take the ID apart, then make it work for subsystems whose names contain a dash themselves.

Split the ID at the dash. Print the subsystem, the unit number as an int, and the ID of the next unit:

Subsystem: ENG
Unit number: 2
Next unit: ENG-3

The pieces from split() are text, and "2" + 1 would crash, so turn the number into an int before you add 1.

Take the ID apart
Works for any ID

Finish stage 1 to unlock this stage.

Some subsystems have a dash in their name: LIFE-SUPPORT-4. The starter code stops with a ValueError, because split("-") cuts at every dash and int("SUPPORT") makes no sense. Split only at the last dash, so it prints:

Subsystem: LIFE-SUPPORT
Unit number: 4
Next unit: LIFE-SUPPORT-5

rsplit("-", 1) splits from the right, and the 1 means "at most once". IDs like ENG-2 must still work.

Split at the last dash
Still works for simple IDs
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

Methods find, count, replace and split

methods-find-replace-split.py
# Searching, counting, and splitting strings

# A snippet of mission log text
report = "Mars rover Curiosity landed. Curiosity is still active."

# .find() returns the index of the first match, or -1 if not found
position = report.find("Curiosity")
print("Curiosity first appears at index:", position)

missing = report.find("Voyager")
print("Voyager index:", missing)  # -1 means not found

# .count() returns how many times a substring appears
mentions = report.count("Curiosity")
print("Curiosity mentioned:", mentions, "times")

# .replace() can clean up or swap text - here we redact a name
public_report = report.replace("Curiosity", "[REDACTED]")
print(public_report)

# .split() breaks a string into a list, using a separator
crew_data = "Mark Watney,Botanist,Ares III,Sol 18"
fields = crew_data.split(",")
print("Fields:", fields)

# We can grab individual pieces by index
print("Name:", fields[0])
print("Role:", fields[1])

# Splitting on whitespace - no argument needed
sentence = "Houston we have a problem"
words = sentence.split()
print("Words:", words)
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:].