replit

Before you start this course, you must login to Replit

If you have forgotten your Replit account user name or password, ask your Coder Coach.

CoderSports


Predict & Run

At the ‘predict’ & ‘run’ stages students work entirely with example code. They should inspect it carefully and write a prediction about what it will do.

They then run the code and compare the result to their prediction.


Substrings

A substring returns a sequence of characters from a string. Python uses slices to do this.

Slices treat strings like lists. Each character is given an index number starting with 0.

[0,1,2,3,4,...]

The slice uses two indexes, the start character and the end character (NOTE the end character is NOT INCLUDED in the slice).

The slice indexes come in brackets after the string or the variable containing the string.

# Outputs 'ompu'
print("Computing"[1:5])

word = "dalmatian"

# Outputs 'ma'
print(word[3:5])

Errors & Misconceptions To Watch Out For

  • The start and end indices are surrounded by square brackets
  • The start and end indices have a colon : between them.
  • The end index is one more than the character you want to stop on.

Hints