String Slicing using a Negative Index || Start : Stop : Step Index || What? How? || Python

String Slicing using a Negative Index || Start : Stop : Step Index || What? How? || Python


String Slicing using a Negative Index || Start : Stop : Step Index || What? How? || Python

This video will discuss WHAT and How of String Slicing using a Negative Index with a lot of EXAMPLES and ANIMATIONS.
Please subscribe to our channel for more such videos.

••••••••••••••••••••••••••••••••••••••••­­­•••••••••••

Video Covers:

— Types of Indexes
— What is a Negative Index?
— How are strings indexed using both Positive and Negative Indexes?
— How are Strings sliced using a Negative Index?
— Scenario 1: No Step Size while slicing using a Negative Index
— Scenario 2: String Slicing using a combination of Positive and Negative Index
— Scenario 3: Slicing using the Positive Indexes and a Negative Step Size
— Scenario 4: How to Reverse a String?


••••••••••••••••••••••••••••••••••••••••­­­•••••••••••

String Indexing | Why? How? || Python Tutorial for Beginners -    • String Indexing | Why? How? || Python…  

String Slicing || Start : Stop : Step Index || What? How? || Python Tutorial for Beginners -    • String Slicing || Start : Stop : Step…  

••••••••••••••••••••••••••••••••••••••••­­­•••••••••••

Watch the full Python Beginner Series here:
   • Learn Python Basic \u0026 Coding  

••••••••••••••••••••••••••••••••••••••••­­­•••••••••••

Music Used :
––––––––––––––––––––––––––––––
Tobu - Candyland [NCS Release] :    • วิดีโอ  
––––––––––––––––––––––––––––––

••••••••••••••••••••••••••••••••••••••••­­­•••••••••••

Thankyou for watching our video.

Keywords :
Machine Learning , Machine Learning Python , Machine Learning Tutorials , ML , Python Tutorial , Machine Learning with Python , Machine Learning tutorial for beginners , How to start Machine Learning , Basics of Machine Learning , ml , Machine learning with Python , Tips and tricks in Python , python programming , python tutorials for Beginners, variables and data types in Python , is python case sensitive , computer programming , How to earn more money , Python Strings , immutable , strings are array , source code , GitHub , immutable object , strings in python, python tutorials for beginners , python strings array , python string advanced , python string literals , string indexing , negative index , reverse index , indexing notation , string indexing out of range error , reverse a string in Python , String operations in Python , Python string programs , string concatenation in python , string functions in Python , python strings , python string code , python code , string slicing in python , string positive index , string negative index , how to reverse a string , reverse string in python , how to be rich , Make money using Python , make money using coding


Content

0.02 -> In today’s video, we will be discussing:
2.23 -> 1) Types of Indexes 2) What is a Negative Index?
5.17 -> 3) How are strings indexed using both positive and negative indexes?
8.98 -> 4) How are strings sliced using a negative index?
12.16 -> 5) Scenario 1: No Step Size while slicing using a Negative Index
16.04 -> 6) Scenario 2: String Slicing using a combination of Positive and Negative Index.
20.66 -> 7) Scenario 3: Slicing using the Positive Indexes and a Negative Step Size
24.8 -> 8) Scenario 4: How to Reverse a String Hey champions, if you are interested in these
29.39 -> topics, then keep on watching. You can subscribe to our channel for more such videos.
33.23 -> First, let’s understand - What exactly is a Negative index?
36.66 -> To understand it, let’s first see what is an index in Python?
40.09 -> Python Strings are an array of bytes. And to access an individual element from this
44.75 -> array, we need to know its position in the array. This position is called an index.
48.98 -> We have covered string indexing in detail in one of our videos. If you are interested
53.79 -> in the topic then the link is in the description box.
56.76 -> An Index in Python can be of two types:
58.7 -> 1) The first type is the Positive index – Here the pointer moves from left to right in the
63.6 -> array. Since it is the default behaviour in Python, hence we can refer to it by index.
69.09 -> The positive index starts with 0. 2) The next type is the negative index – As
73.259 -> the name suggests, the functionality of this index is opposite to that of the positive
77.899 -> index. Here the pointer moves from right to left in the array. It is also known as the
83.029 -> Reverse index. To depict the negative index, a negative sign is added just before the index
88.009 -> and starts with -1.
90.009 -> Now that we know about both the indexes in python, Let’s see how are strings indexed
94.99 -> using both of these indexes. We have a string ‘Champ’ and here is how
98.899 -> it is indexed using a positive and a negative index. As you can see a positive index starts
103.929 -> from the left of the string and has 0 as the starting index, while a negative index starts
109.179 -> from the right of the string and has -1 as the starting index.
112.619 -> Interesting, isn’t it?
114.459 -> We have made a separate video on String Slicing with a positive index on this channel. If
118.61 -> you are interested in the topic then the link is in the description box.
121.779 -> Let’s now move on to our next topic which is how to slice a string using a negative
125.92 -> index. Slicing using a negative index is quite similar
129.149 -> to that done using a positive index, but one of the distinguishing features between both
134.02 -> is that when the slicing is done using a negative index, the step size is negative while when
139.32 -> the slicing is done using a positive index, the step size is positive. This is important
143.92 -> because the step size will direct the direction of the pointer. If the step size is positive,
148.47 -> then the pointer will move from left to right in the array but if this index is negative
152.65 -> then the pointer will move from right to left in the array.
155.55 -> Let’s look at an example to better understand this concept.
158.34 -> For our example, we have taken a string ‘Learning Champion’ and to slice it using a negative
163.35 -> index we have given mystring[-1:-5:-1] as the slicing command to Python. Once we execute
171.62 -> this command, it will return ‘noip’ as the output. Here, -1 is the step size which
177.12 -> will move the pointer from right to left, -1 is the start index which is the index of
181.63 -> the last character in the original string and -5 is the stop index which is the index
186.27 -> of letter m in the string but as we all know that this index will not be included in the
191.24 -> output. Also, note that in this example start index is greater than the stop index which
196.32 -> is not the case when we do slicing using a positive index.
199.57 -> Hence, whenever we want to do slicing using a negative index, we have to keep 2 things
204.02 -> in mind : 1) The first is that step size should be negative
207.55 -> 2) And second is that the start index should always be greater than the stop index because
212.74 -> now we are moving from right to left. I hope the example made everything easier
216.91 -> to understand. Let’s now look at a few other scenarios
219.99 -> Scenario 1: No Step Size while slicing using a Negative Index
223.9 -> For this scenario, we have taken -1 as the starting index and -5 as the stop index but
229.89 -> have not defined the step size. Since we have not mentioned any step size, Python will assume
234.79 -> it as 1 by default. After executing the command, we will notice
238.64 -> that the Python has returned Nothing! But why? Isn’t it odd?
242.84 -> This is because the default step size is 1 which will move the pointer from left to right.
247.29 -> But when we gave the starting index as -1 and the ending index as -5, we are telling
253.27 -> the Python pointer to move from right to left as the starting index is greater than the
257.509 -> stop index. Python did not return anything because of the contradicting inputs.
262.61 -> Scenario 2: String Slicing using a combination of Positive and Negative Index.
267.039 -> for our second scenario, We have given mystring[-1:13:-1]. When we run this command, Python will return
275.319 -> ‘noi’ as the result. But shouldn’t this be an error? Here the starting index is not
279.93 -> greater than the stop index but still, Python has given us a valid output. This is because
284.909 -> each element is represented by a negative as well as a positive index. It doesn’t
289.18 -> matter which index you mention as long as the start index is before the stop index when
293.75 -> we specify -1 as the step size. Let’s move on to the next scenario, which
298.419 -> is Scenario 3: Slicing using the Positive Indexes
301.27 -> and a Negative Step Size In this scenario, we have written mystring[7:0:-1]
308.199 -> and when we execute this command the output will be a substring that will start from the
312.919 -> 7th index till the 0th index but will not include the element at the 0th index. Hence,
318.37 -> we can also slice the string from right to left using both the positive start and stop
323.219 -> index as long as the step size is negative.
325.55 -> Scenario 4: How to Reverse a String?
327.55 -> Let’s move on to the last scenario, where we try to reverse the string we have.
332.24 -> For it, we have written mystring[::-1] and the output is the reverse of the initial string
338.28 -> we gave. Here notice a few things. First, as we did not mention the starting index,
343.3 -> Python has assumed it as -1 by default based on the step size we have given and, second,
348.719 -> we also did not mention the stop index, Python has assumed it as the starting of the string
353.819 -> by default based on the step size we have given. Here the step size is the governing
357.969 -> factor that will decide the default values of start and stop indexes along with moving
362.639 -> the pointer from right to left. That’s it for today’s video. I hope all
366.319 -> the examples made the concept easier for you. You can share your thoughts or questions in
370.479 -> the comment box. Subscribe for more such videos. Bye and meet you in the next one.

Source: https://www.youtube.com/watch?v=FNg4TKUIC60