Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today, we're going to learn about string indexing in Python. Remember, strings are sequences of characters where each character can be accessed using an index. For instance, in the string 'Python', can anyone tell me what character is at index 0?
'P' is at index 0.
What about the last character?
Good question! That's where negative indexing comes into play. The last character can be accessed using index -1. So, what do you think is at index -1?
'n'!
Exactly! So remember, we can always count from the end using negative indices.
Now, let's move on to string slicing. Slicing allows us to extract a substring from a string. The general syntax is text[start:stop], but if we omit start, it begins from the start of the string. Can anyone give me an example?
If I use 'Programming'[0:6], I'll get 'Progra'!
What if I use 'Programming'[:4]?
You’ll get 'Prog'. Good! It's important to remember that the stop index is exclusive. Let's try one more example, like 'Programming'[4:]. What does that yield?
'ramming'!
You all are grasping this quickly! To remember, think 'start:stop, but stop is not included!'
Now let’s apply what we learned with some exercises. How would you retrieve the first, middle, and last character from the string 'Python'? Who can take a shot?
I would do text[0] for the first character, then find the middle index by getting the length of the string.
So, len('Python') // 2 gives us 3, and text[3] gives us 'h'. For the last character, text[-1] is 'n'!
Fantastic! Now let's slice part of the string by choosing different start and stop values to get substrings.
Indexing starts at zero, slicing gives you bits like a hero.
Imagine a pizza: the slices represent portions of your string, and each topping is like a character, accessed by your order number!
I-S slice (Index - Slice). Remember: I starts at 0, and S gives you pieces.
ISS: Index, Slice, Select - you index first, slice to select your characters in the string.
Term
What does indexing do in Python?
Click to flip
Term
What is the syntax for slicing a string?
Click to flip
Term
How do you access the last character in a string?
Click to flip
Accessing individual characters in a string using their position.
Extracting a substring from a string using the syntax text[start:stop].
An index that counts from the start of the string, starting at 0.
An index that counts backward from the end of the string, starting at -1.
This section covers how strings in Python can be accessed and manipulated using indexing and slicing techniques.
String indexing and slicing are essential skills for Python string manipulation. Indexing allows access to individual characters in a string using their position, while slicing enables extraction of substrings. This section not only explains these techniques in detail but also provides examples for better understanding.
In Python, strings are sequences of characters that are indexed starting from 0. For instance, in the string text = 'Python', the character P is retrieved with text[0] and n can be retrieved with text[-1]. Indexing can be both positive and negative, allowing users to access characters from the start or the end of the string efficiently.
Slicing, on the other hand, allows users to extract specific portions of a string. The syntax for slicing is text[start:stop], where stop is excluded from the slice. For example, text[0:6] results in Progra, text[:4] gives Prog, and text[4:] returns ramming. This versatility makes string manipulation straightforward in Python, contributing significantly to data handling and formatting tasks.