Learning Objectives
- Understand the concept of "Zero-Based Numbering" and why computers count differently than humans.
- Learn how to grab one single, specific character from a string using Indexing.
- Discover how to extract large chunks or pieces of a string using Slicing.
How Computers Count
Imagine you are standing in front of a row of lockers. If someone asks you to open the first locker, you would walk up to the locker labeled with the number 1. Humans have always started counting at one.
Computers, however, do not think like humans.
When a computer looks at a sequence of data—like a String of text—it starts counting at Zero. This is known in programming as Zero-Based Indexing. It is one of the most fundamental rules of software development, and once you get used to it, it will feel completely natural.
Let's look at the word "PYTHON". To a human, the letter P is the 1st letter. To Python, the letter P is at position 0.
- P -> Position 0
- Y -> Position 1
- T -> Position 2
- H -> Position 3
- O -> Position 4
- N -> Position 5
Indexing: Grabbing a Single Letter
Now that we know how Python counts, how do we actually tell Python to grab a specific letter from our text?
We use **square brackets []** right after our variable name. Inside the brackets, we put the position number (the index) of the character we want.
Imagine you are building an educational platform named Acadestine. You want to generate a user's avatar icon by grabbing the very first letter of their username.
Here is how you do it:
What happens if I ask for a letter that doesn't exist?
If a word only has 5 letters, and you ask Python for word[100], Python will panic and completely crash your program with an IndexError. It will say: String index out of range. Always make sure the position you are asking for actually exists!
Negative Indexing: Counting Backwards
What if you want to grab the very last letter of a string, but you have no idea how long the string is?
Imagine you are building an email marketing platform, and you want to check the last character of a massive string of data. You wouldn't want to manually count thousands of characters just to find the final index number!
Python provides a brilliant shortcut: Negative Indexing.
If 0 is the start of the string, you can use negative numbers to count backwards from the end.
-1always grabs the very last character.-2grabs the second-to-last character.
Slicing: Extracting Chunks of Text
Grabbing one letter is great, but what if you want to extract an entire chunk of text? Maybe you have a user's full name, and you only want to extract their first name.
We do this using a technique called Slicing.
Slicing also uses square brackets, but instead of one number, we give Python a start and a stop position, separated by a colon :.
The syntax looks like this: string[start:stop]
Here is the most important rule of slicing: Python will start at your first number, and stop just before the second number. It goes up to, but does not include, the stop index.
Slicing Shortcuts
Programmers love saving time. If you want to start slicing from the very beginning of the string, you can completely leave the first number blank! Python will assume you mean 0.
Similarly, if you want to slice all the way to the very end of the string, you can leave the second number blank.
Slicing with a Step
There is one more secret trick you can do with slicing. You can add a third number, called the Step.
The full syntax is: string[start:stop:step]
By default, the step is 1, meaning Python grabs every single character in a row. But if you change the step to 2, Python will grab every other character (skipping one each time).
Lesson Summary
You just learned how to manipulate and dissect text with precision! Let's quickly review the core concepts:
- Zero-Based Indexing: Computers always start counting at 0, not 1. The first letter of a string is always at index
[0]. - Indexing: Using square brackets with a single number to extract a single character (e.g.,
name[2]). - Negative Indexing: Using negative numbers to count backwards from the end of the string.
[-1]is always the last character. - Slicing: Extracting a chunk of text using a colon (e.g.,
text[start:stop]). Remember that thestopposition is not included in the final result! - Step: Adding a third number to your slice lets you skip characters or even reverse the string entirely using
[::-1].