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.
Auf dieser Seite können Sie das Online-Video Python String Data Type Slicing and Indexing | String Slicing | String Indexing | Positive Slicing mit der Dauer online in guter Qualität ansehen, das der Benutzer Anuurag Edlabadkar 04 Januar 2022 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 6 Mal angesehen und es wurde von 0 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!