Enrol to start learning
Reading is open to everyone. Enrolling is free, and it is what unlocks the audio lessons, practice tests and progress tracking.
7.2. String Indexing and Slicing
Interactive Audio Lesson
Unlock the classroom podcast
The transcript is above and free to read. A free account plays the conversation back.
Create a free accountToday, 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.
Unlock the classroom podcast
The transcript is above and free to read. A free account plays the conversation back.
Create a free accountNow, 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!'
Unlock the classroom podcast
The transcript is above and free to read. A free account plays the conversation back.
Create a free accountNow 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.
Overview
Short Summary
This section covers how strings in Python can be accessed and manipulated using indexing and slicing techniques.
Medium Summary
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.
Detailed Summary
Detailed Summary
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.
Audio Book
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free accountPython strings are indexed, starting from 0.
✅ Indexing:
text = "Python"
print(text[0]) # Output: P
print(text[-1]) # Output: nDetailed Explanation
Indexing in Python refers to accessing specific characters in a string using their position. The index of the first character is 0, the second character is 1, and so on. You can also use negative indexing, where -1 refers to the last character, -2 to the second last, and so forth. In the example provided, text[0] fetches the first character 'P', and text[-1] fetches the last character 'n'.
Examples & Analogies
Think of a string like a series of boxes lined up in a row, where each box represents a character. The first box is numbered 0, the next 1, and so on. You can point to any box using its number to see what's inside. Similarly, if you use negative numbers, it's like counting backwards from the end of the row.
Unlock the audio lesson
The script is above and free to read. A free account plays it back, in the voice you pick.
Create a free account✅ Slicing:
text = "Programming"
print(text[0:6]) # Output: Progra
print(text[:4]) # Output: Prog
print(text[4:]) # Output: rammingDetailed Explanation
Slicing allows you to obtain a substring from a string by specifying a range of indices. The syntax is text[start:end], where 'start' is the index of the first character to include, and 'end' is the index where it stops (not included). For instance, text[0:6] extracts characters from index 0 to 5, giving 'Progra'. If you leave 'start' empty (like text[:4]), it assumes the beginning of the string, and if you leave 'end' empty (like text[4:]), it takes all characters from the specified starting point to the end of the string.
Examples & Analogies
Imagine you're reading a book. If you want to read specific pages, you can tell someone to start from page 1 and stop before page 7, which would be like slicing the string. If you want to read from page 1 to the end, you'd just say start at page 1. This way of extracting parts of a string lets you focus on just the information you need, similar to taking a snippet from a larger story.
--
Key Concepts
Core takeaways and short definitions to help you quickly recall the key ideas from this section.
String Indexing: Accessing characters in a string via their position.
String Slicing: Extracting parts of a string using specified start and end indices.
Negative Indexing: Accessing characters from the end of a string using negative numbers.
Examples
Memory Aids
Interactive tools to help you remember key concepts
Stories
Flash Cards
Glossary
Indexing
Accessing individual characters in a string using their position.
Slicing
Extracting a substring from a string using the syntax text[start:stop].
Positive Index
An index that counts from the start of the string, starting at 0.
Negative Index
An index that counts backward from the end of the string, starting at -1.