Python - Indexing and Slicing
Indexing & Slicing is an important concept in Python, especially when we use strings.
Indexing :
WKT, string is nothing but a sequence of characters.
So, each character has a position namely index and accessing their position in that particular string is known as indexing.
In Python, we have zero based indexing i.e., the first character of a string has an index(position) of 0 rather than having one, then the second character has an index(position) of 1 and so on.
For example,
> H E L L O W O R L D
> 0 1 2 3 4 5 6 7 8 9
This is known as positive indexing as we have used only positive numbers to refer the indices.
You may ask that "Then, we have negative indicing too??"
Ofc, but in here we do not have zero as the first position as it ain't a negative number.
Negative indexing allows us to access characters from the end of the string i.e., the last character has an index of -1, the second last character has an index of -2, and so on.
> H E L L O W O R L D
> -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
word = "HELLOWORLD"
print(word[0])
print(word[5])
H
W
Similarly,
print(word[-1])
print(word[-6])
D
0
That's it about indexing.
Slicing :
Think of slicing a string like cutting a slice of cake from a whole cake. We can specify where to start cutting (index), where to finish (end index), and even how big each slice should be (step). This way, we can create smaller portions of the cake (or string) exactly how we like them!
In Python, slicing a string lets us grab specific parts of it by specifying where to start and where to end within the string.
So, for instance, if message contains "HELLOWORLD", then message[3:7] gives you "LOWO" because it starts at index 3 ('L') and ends just before index 7 ('D'). This way, we can extract any part of a string we need!
- The basic syntax for slicing is,
string[start:stop]
- The start index is where the slice begins, and this index is inclusive.
- The stop index is where the slice ends, but this index is exclusive, meaning the character at this index is not included in the slice.
text = "HappyBirthday"
print(text[0:5])
print(text[5:13])
Happy
Birthday
When slicing a string in Python, we can simply omit the start or stop index to slice from the beginning or to the end of the string.
It's as straightforward as that!
- Slicing with a step,
To specify the interval between characters when slicing a string in Python, just add a colon followed by the step value:
string[start:stop:step]
This allows to control how we want to skip through the characters of the string when creating a slice.
message = "HELLOWORLD"
print(message[1::2])
EORL
message[1::2] starts slicing from index 1 ('E') to the end of the string, with a step of 2.
Therefore, it includes characters at indices 1, 3, 5, and 7, giving us "EORL".
Until, we saw about positive slicing and now let's learn about negative slicing.
- Negative Slicing :
- A negative step allows you to slice the string in reverse order.
- Let us slice from the second last character to the third character in reverse order
message = "HELLOWORLD"
print(message[-2:2:-1])
ROWOL
Let's look into certain questions.
#Write a function that takes a string and returns a new string consisting of its first and last character.
word = "Python"
end = word[0]+word[5]
print(end)
Pn
#Write a function that reverses a given string.
word = "Python"
print(word[::-1])
nohtyP
#Given a string, extract and return a substring from the 3rd to the 8th character (inclusive).
text = "MichaelJackson"
print(text[3:9])
haelJa
#Given an email address, extract and return the domain.
email = "hello_world@gmail.com"
domain = email[:-10]
print(domain)
hello_world
#Write a function that returns every third character from a given string.
text = "Programming"
print(text[::3])
Pgmn
#Write a function that skips every second character and then reverses the resulting string.
text1 = "Programming"
print(text1[::-2])
gimroP
#Write a function that extracts and returns characters at even indices from a given string.
text = "Programming"
print(text[::2])
Pormig
Allright, that's the basic in here.
.....