Home > Backend Development > Python Tutorial > Python - Indexing and Slicing

Python - Indexing and Slicing

王林
Release: 2024-07-25 09:15:42
Original
321 people have browsed it

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
Copy after login

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
Copy after login
word = "HELLOWORLD"

print(word[0])
print(word[5])

H
W

Copy after login

Similarly,

print(word[-1])
print(word[-6])

D
0
Copy after login

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]
Copy after login
  • 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  
Copy after login

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]
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

#Write a function that reverses a given string.

word = "Python"
print(word[::-1])

nohtyP
Copy after login

#Given a string, extract and return a substring from the 3rd to the 8th character (inclusive).

text = "MichaelJackson"
print(text[3:9])

haelJa
Copy after login

#Given an email address, extract and return the domain.

email = "hello_world@gmail.com"
domain = email[:-10]
print(domain)

hello_world
Copy after login

#Write a function that returns every third character from a given string.

text = "Programming"
print(text[::3])

Pgmn
Copy after login

#Write a function that skips every second character and then reverses the resulting string.

text1 = "Programming"
print(text1[::-2])

gimroP
Copy after login

#Write a function that extracts and returns characters at even indices from a given string.

text = "Programming"
print(text[::2])

Pormig
Copy after login

Allright, that's the basic in here.

.....

The above is the detailed content of Python - Indexing and Slicing. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template