String Slicing
Extract parts of a string (substrings) using slicing: string[start:end]
. The `start` index is included, but the `end` index is excluded. Omitting `start` defaults to 0, omitting `end` defaults to the string's length.
String Methods
Strings have built-in functions called methods (like .upper()
, .lower()
) that perform actions or return new strings. Note: Methods don't change the original string; they return a modified copy.
F-Strings (Formatting)
F-strings (formatted string literals), starting with f"..."
, provide a clean way to embed expressions and variables directly inside strings using curly braces {}
. This is essential for creating dynamic text output.
Example f-string code: f"Hello, {name}! Welcome to Module {module_num}."
Advanced String Missions!
Time to combine slicing, methods, and maybe even f-strings! Complete these tasks using `print()` in the editors.
Mission 1: Slice and Shout
Given the string `"python"`, write a command to `print` the first 3 characters (`"pyt"`) converted to uppercase (`"PYT"`).
Mission 2: Formatted Intro
Create two variables: `subject = "AI"` and `level = 3`. Then, use an f-string with `print()` to output the exact text: `Subject: AI, Level: 3`.
Mission 3: Lower End Slice
Given the string `"ADVANCED PYTHON"`, write a command to `print` the last 6 characters (`"PYTHON"`) converted to lowercase (`"python"`). Hint: Negative indices slice from the end.
Module 3 Advanced Practice Complete!
Fantastic! You've mastered more complex string operations in Python. These skills are crucial for data processing in AI.
Check out the Video Lesson or return to the Course Menu.