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.
Power Up: Left & Right
Python doesn't have LEFT() or RIGHT() like other languages. We use slices!
Predict Output
What is the value of word[1]?
print(word[1])
Zero Indexed!
Last Letter
"Change the index to print the last letter of the string using [-1]."
Off by One
"Extract exactly 'Code'. Remember the stop number is NOT included!"
User Gen
Independent Search Pattern
- Inputs: Ask for
firstandlastnames. - Username:
first[0:3]+last[0:2]. - Final Output: Print the username in UPPERCASE.