Chapter 14: String Indexing and Slicingยถ
๐ Open Notebookยถ
๐บ Video Tutorialยถ
String indexing in Python is easy โ๏ธ (6:17)
๐ What Youโll Learnยถ
Access and extract parts of strings using indexing and slicing - essential for text processing!
๐ฏ Learning Objectivesยถ
Understand zero-based indexing
Use positive and negative indices
Slice strings with [start:end:step]
Reverse strings and extract substrings
๐ Concept Explanationยถ
Indexingยถ
Each character in a string has a position (index) starting from 0.
String: "PYTHON"
Index: 0 1 2 3 4 5
Negative:-6-5-4-3-2-1
Slicing Syntaxยถ
string[start:end:step]
start: Beginning index (inclusive)
end: Ending index (exclusive)
step: Interval between characters
๐ก Examplesยถ
Single Character Accessยถ
word = "Python"
print(word[0]) # P (first character)
print(word[3]) # h (fourth character)
print(word[-1]) # n (last character)
print(word[-2]) # o (second to last)
Slicing Rangesยถ
text = "Programming"
print(text[0:4]) # "Prog" (index 0 to 3)
print(text[3:7]) # "gram" (index 3 to 6)
print(text[:4]) # "Prog" (start to index 3)
print(text[4:]) # "ramming" (index 4 to end)
print(text[:]) # "Programming" (entire string)
Step Parameterยถ
numbers = "0123456789"
print(numbers[::2]) # "02468" (every 2nd char)
print(numbers[1::2]) # "13579" (every 2nd, starting at 1)
print(numbers[::3]) # "0369" (every 3rd char)
Reversing Stringsยถ
word = "Python"
print(word[::-1]) # "nohtyP" (reversed)
# Practical use: palindrome checker
word = "racecar"
if word == word[::-1]:
print("It's a palindrome!")
โ๏ธ Practice Exercisesยถ
Extract first 3 characters from a string
Get the last 4 characters
Extract every other character
Reverse a userโs name
Check if a string is a palindrome
Extract domain from email (after @)
Get file extension from filename
๐ฎ Real-World Examplesยถ
Credit Card Formatterยถ
card = "1234567812345678"
formatted = f"{card[0:4]}-{card[4:8]}-{card[8:12]}-{card[12:16]}"
print(formatted) # 1234-5678-1234-5678
# Hide middle digits
hidden = f"{card[0:4]}-****-****-{card[-4:]}"
print(hidden) # 1234-****-****-5678
Extract Informationยถ
date = "2024-01-15"
year = date[0:4] # "2024"
month = date[5:7] # "01"
day = date[8:10] # "15"
print(f"Year: {year}, Month: {month}, Day: {day}")
URL Parserยถ
url = "https://www.example.com/page"
protocol = url[0:5] # "https"
domain = url[12:23] # "example.com"
path = url[23:] # "/page"
๐ Common Patternsยถ
First/Last N Charactersยถ
text = "Hello World"
first_5 = text[:5] # "Hello"
last_5 = text[-5:] # "World"
Remove First/Last N Charactersยถ
text = "Hello World"
without_first = text[1:] # "ello World"
without_last = text[:-1] # "Hello Worl"
Every Nth Characterยถ
text = "ABCDEFGHIJ"
every_2nd = text[::2] # "ACEGI"
every_3rd = text[::3] # "ADGJ"
๐ Challenge Projectsยถ
Social Security Number Formatter: XXX-XX-XXXX
Phone Number Parser: Extract area code, prefix, line number
Acronym Maker: Take first letter of each word
Text Truncator: Limit text to N characters + โโฆโ
Caesar Cipher: Shift characters by N positions
๐ Slicing Tricksยถ
Copy a Stringยถ
original = "Python"
copy = original[:] # Creates a copy
Reverse Wordsยถ
sentence = "Hello World"
words = sentence.split()
reversed_words = [word[::-1] for word in words]
result = " ".join(reversed_words)
print(result) # "olleH dlroW"
Extract Extensionยถ
filename = "document.pdf"
extension = filename[filename.rfind('.')+1:]
print(extension) # "pdf"
๐ Key Takeaways from Videoยถ
Strings are text data enclosed in quotes
Follow along with the video for hands-on practice
Experiment with the code examples to deepen understanding
๐ก These points cover the main concepts from the video tutorial to help reinforce your learning.
๐ Next Chapterยถ
Continue to Chapter 15: Format Specifiers to learn advanced string formatting!