Python String Data Type Slicing and Indexing | String Slicing | String Indexing | Positive Slicing

Pubblicato il: 04 gennaio 2022
sul canale di: Anuurag Edlabadkar
6
0

The Python string data type is a sequence made up of one or more individual characters that could consist of letters, numbers, whitespace characters, or symbols. As the string is a sequence, it can be accessed in the same ways that other sequence-based data types are, through indexing and slicing.

Indexing

Indexing means referring to an element of an iterable by its position within the iterable. Each of a string’s characters corresponds to an index number and each character can be accessed using their index number.

We can access characters in a String in Two ways :

Accessing Characters by Positive Index Number

Accessing Characters by Negative Index Number
1. Accessing Characters by Positive Index Number:
In this type of Indexing, we pass a Positive index(which we want to access) in square brackets. The index number start from index number 0 (which denotes the first character of a string).

Rules for positive indexing and positive slicing.
1. Default value of first element is zero; first element is smallest element.

2. Default value of second element is length of string; second element is largest element.
3. Default value of third element is always positive one (1); third element desides the flow, from left to right.
4. Rule for second element is (n - 1); where n is length of sub-string, after subtraction it return the "index value".
5. Rule for third element is (n - 1); where n is position, after subtraction it return the "step value"/skip value.

2. Accessing Characters by Negative Index Number :

In this type of Indexing, we pass the Negative index(which we want to access) in square brackets. Here the index number starts from index number -1 (which denotes the last character of a string).

Rules for negative indexing and negative slicing.
1. Default value of first element is -ve 1; first element is largest element.
2. Default value of second element is -ve length of string; second element is smallest element.
3. Default value of third element is always negative one (-ve 1); third element desides the flow, from right to left.
4. Rule for second element is (n + 1); where n is length of sub-string, after subtraction it return the "index value".
5. Rule for third element is (n + 1); where n is position, after subtraction it return the "step value"/skip value.


In questa pagina del sito puoi guardare il video online Python String Data Type Slicing and Indexing | String Slicing | String Indexing | Positive Slicing della durata di online in buona qualità , che l'utente ha caricato Anuurag Edlabadkar 04 gennaio 2022, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 6 volte e gli è piaciuto 0 spettatori. Buona visione!