Tutorial 14/15 ADVANCED TECHNIQUES

Python: Strings

Strings are lists of characters. You can slice them!

The Hook

Think of a String like a bead necklace.

Each letter is a bead. You can pick one bead (Index) or cut a section of the necklace (Slice).

Remember: Computers start counting from Zero. Index 0 is the first letter!

Worked Example

Extracting parts of a Secret Code.

# Index counts: 0, 1, 2, 3, 4, 5
code = "PYTHON"
print(code[0]) # Gets "P"
print(code[0:3]) # Gets "PYT" (Stops BEFORE 3)

Power Up: Left & Right

Python doesn't have LEFT() or RIGHT() like other languages. We use slices!

# LEFT(3) -> First 3
text[:3]
# RIGHT(3) -> Last 3
text[-3:]
Logic Gate

Predict Output

What is the value of word[1]?

word = "GALAXY"
print(word[1])

Zero Indexed!

Level 37: Bronze

Last Letter

"Change the index to print the last letter of the string using [-1]."

> bronze_stream_standby
Level 38: Silver

Off by One

"Extract exactly 'Code'. Remember the stop number is NOT included!"

> debugger_module_monitoring
Level 39: Gold

User Gen

Independent Search Pattern

  • Inputs: Ask for first and last names.
  • Username: first[0:3] + last[0:2].
  • Final Output: Print the username in UPPERCASE.
> computation_vector_online