In this video, we look at how to pull out specific characters and extract entire chunks of text from a string using indexing and slicing in Python.
You'll learn how Python index numbers work (starting at zero), how to read strings backward using negative index values, how the slice range boundaries function (why the stop boundary is excluded), and how to search strings using the find and count methods.
📝 CODE WRITTEN IN THIS VIDEO:
--------------------------------------
Positive and Negative Indexing
text = "Python"
print(text[0]) # "P"
print(text[-1]) # "n"
Slicing ranges
print(text[0:4]) # "Pyth" (excludes index 4)
print(text[:4]) # "Pyth"
print(text[2:]) # "thon"
print(text[:]) # "Python"
Find and Count methods
phrase = "Python Programming"
print(phrase.find("Pro")) # 7
print(phrase.count("m")) # 2
🧠 BONUS CHALLENGES:
--------------------------------------
Test your indexing and slicing knowledge! Comment your answers below.
Challenge 1:
What does this code print?
text = "Developer"
print(text[-3:])
Options:
A) "per"
B) "ope"
C) "lop"
Challenge 2:
What is the output of this code?
phrase = "Mississippi"
print(phrase.count("i") + phrase.find("s"))
Options:
A) 6
B) 8
C) 7
#python #programming #coding #pythontutorial #learnpython #compsci