Python for Beginners, Part 11: String Slicing by@cleverprogrammer

Python for Beginners, Part 11: String Slicing

Strings are one of the primitive data types of python, as we have seen in previous part of the series. String indexes start with 0 and string slicing in python programming is used to fetch a part of the string (substring) from a given string by slicing it from a 'start' index until a 'stop' index.
image
Clever Programmer HackerNoon profile picture

Clever Programmer

Clever Programmer is a community with over 100,000+ students who are learning to code by building real world projects.

facebook social icontwitter social iconlinkedin social iconyoutube social iconinstagram social icon

Now let's talk about string slicing.

In case you missed it, here are the previous parts of the series

Transcript

0:00 There my beautiful children,

0:01 in this video we're gonna talk about string slicing.

0:04 What is string slicing?

0:06 We're not talking about cake slicing, so let's get started.

0:09 Well a string as you remember

0:11 was that thing in the green here,

0:13 but that's just really specific

0:15 to the IDE that we're using,

0:17 if you're using any other text editor it's not necessarily

0:19 gonna be green.

0:20 So the string is just something in quotes.

0:22 Either it's single quotes or double quotes

0:24 as long as it's consistent,

0:27 Can't have a single quote ending with a double quote.

0:30 They gotta match.

0:32 Okay.

0:34 So we have to match with the quotes,

0:37 that's your string, right.

0:39 So what we want to do now is slice those strings.

0:43 Well, what does that mean?

0:46 Imagine I gave you an email address.

0:48 Email is equal to, I don't know,

0:52 rafeh at gmail dot com,

0:56 something like that.

0:57 Well, a common thing that people have to do when they

1:00 are getting this data back is,

1:03 well, this is the username or something,

1:08 this part is the Gmail thing,

1:13 and then this is the dot com part, right.

1:16 So like, let's say we just want the domain or whatever,

1:19 and here we simply want like the username.

1:22 So how would we do that?

1:24 Well, we can use a little bit of string slicing.

1:27 So let's just start with a simple example.

1:29 Let's take hello, and let's say we want to get the first

1:32 Let's take hello, and let's say we want to get the first

1:35 letter from hello, we wanna get just the H.

1:38 Well, how you do it is you index by opening a bracket,

1:42 and closing a square bracket.

1:44 And if I do zero, it'll get me

1:48 the first letter of that, okay.

1:51 So H in this case is zero, right.

1:53 So H in this case is zero, right.

1:55 The E is one, in Python most of the things start from zero.

1:59 So we go all the way up to this O here,

2:02 which is a four, okay.

2:07 So we take this hello and

2:12 whoops

2:13 let's take this hello and let's simply do a 1,

2:16 let's take this hello and let's simply do a 1,

2:19 and that'll give me the E, okay.

2:21 Now another thing I want to show you guys in string slicing,

2:24 besides doing, okay that index,

2:27 right, this passing in the index one,

2:31 will give me the second letter.

2:32 What if we want to do something cooler

2:33 like get a chunk of code?

2:36 Well, how indexing works is it's start, colon stop.

2:39 Well, how indexing works is it's start, colon stop.

2:43 Okay, colon stop.

2:46 So what you can do is you can say hello.

2:49 Oh, I keep doing that.

2:51 Hello, and you can say start from zero, and end at two.

2:54 Hello, and you can say start from zero, and end at two.

2:57 Not including two, but end at two.

3:01 So include zero, but don't include the two.

3:04 Which means that you really get the zeroth str um

3:07 zeroth index

3:09 and the first index.

3:11 Right, so whatever the letter is at the zeroth index,

3:13 and whatever the letter is at the first index.

3:16 So in this case we'll get an H E, okay.

3:21 How you can read this is just,

3:22 give me the first two letters, that's how I read it.

3:25 So you can just say give me the first two letters.

3:28 Now when you're starting off from zero,

3:30 by default index starts at zero.

3:33 So if you don't actually say this at all,

3:35 and you just remove the zero,

3:36 you're gonna get back H E, okay.

3:41 Everything that I'm showing you guys here,

3:44 play around with it, okay?

3:45 Don't just sit here and watch my videos.

3:48 I don't want you to be a passive learner,

3:49 please, please actively do this with me.

3:52 please, please actively do this with me.

3:55 Or pause my video and do this.

3:56 I want you to spend more time doing

3:58 and less time looking at it, okay.

4:00 Because it seems really simple when you're watching it,

4:03 but if you don't put it into action and do things

4:05 that make sense to you,

4:07 if you don't turn it into a game for yourself where you're

4:10 doing something, you know like

4:11 you making fun little stupid projects,

4:14 using what I'm showing you,

4:17 you're not gonna be able to remember it.

4:19 That's really what my problem with Codecademy is,

4:22 a lot of people go through Codecademy,

4:24 but the problem that ends up happening is that

4:27 they just go through it at a really fast pace

4:30 and they don't really have much

4:31 projects that they get to do there,

4:33 and not a lot of stuff that they get to do on their own,

4:36 so the most common complaint you hear from people

4:39 who take the Codecademy course will be like,

4:41 "It was great, I took the whole thing!"

4:42 But, yeah, right now, I can't write a single line of code,

4:46 okay, so it's not impressive the higher

4:49 you are in really abstract levels

4:51 introduction to classes,

4:52 "Oh yeah, I know all about function,

4:54 I'm in introduction to classes."

4:55 Write a function.

4:57 Write a function that takes in three inputs

4:58 and does dadada, print something out and sort in order.

5:02 "Pfft, I don't know how to do that, I'm totally lost."

5:05 Right.

5:06 Don't be one of those people,

5:07 make sure you build strong fundamentals,

5:09 and your coding level is functional at all times, right.

5:13 So even if I'm teaching you really basic stuff,

5:15 make sure you can do something with it.

5:17 That's the only way you'll get better

5:19 and make sure you have fun while doing it.

5:21 Anyway, let's get back to it.

5:22 So, hello, right.

5:26 Make up your own strings and break them up

5:28 and see what you can make up from what I have shown you here

5:31 and experiment, what if I change the end,

5:35 what if I change the stop part of it,

5:37 and what if I change the start part of it,

5:39 what if I don't put anything in the stop part, what happens?

5:41 So let's try that.

5:42 So hello and let's put in nothing, for both parts,

5:46 So hello and let's put in nothing, for both parts,

5:48 let's see what happens.

5:49 You just get the whole string, by default,

5:52 it starts from zero and by default

5:53 it ends at the last index.

5:57 Okay, so, it's the same thing as saying zero

6:01 to if you count it up, zero, one, two, three, four, right?

6:05 to if you count it up, zero, one, two, three, four, right?

6:06 O is four, so I'll say four here, that will not give me it

6:07 O is four, so I'll say four here, that will not give me it

6:10 I have to say five.

6:13 Right, because it will not include the four,

6:16 if I say four in will go up to but won't include the four.

6:19 For example,

6:21 see?

6:22 So we have to do five, okay?

6:26 So that helps us get to the end of the string.

6:30 Now what if I wanna get the last letter from hello?

6:33 Or anything, if I wanna get the last letter,

6:36 I can do something like this.

6:39 Negative.

6:41 Okay, I can also slice by a negative number.

6:45 So if I wanna get hell from hello,

6:47 I can do hello and I can say go from zero

6:50 I can do hello and I can say go from zero

6:55 and let's try see what happens if I put negative one here.

7:00 Right?

7:01 So another way I.

7:02 Basically, what I'm saying is,

7:04 start from the zeroth part go up to

7:09 the last part but not including the last part,

7:12 so that gave me hell.

7:14 Another way to write it is by putting a four here, okay.

7:17 Another way to write it is by putting a four here, okay.

7:21 Another way to write it is not having a zero here.

7:23 Another way to write this is not having a zero here.

7:26 That all gives you hell.

7:27 So you see, play around with all those.

7:29 I'm gonna now show you another thing

7:32 that you can slice with and that'll also blow your mind.

7:36 Slicing is very important, it's a very common procedure,

7:40 so it's pretty go to learn it because,

7:42 let's say you are trying to make an app

7:45 which goes on craigslist and pulls

7:47 a lot of data from craigslist,

7:49 so like pull up ads, so that ads, like

7:51 you might get the data back in a string form.

7:54 So you might get something like,

7:56 XBOX 360 pricing is,

8:02 it's price is $10 and

8:05 that's stupid, it's not $10, right,

8:07 you might get it for $150 nowadays,

8:11 2016 the value of it has significantly dropped,

8:15 and then let's say it says the condition type,

8:18 so the condition type is new, right.

8:22 So this is the

8:26 data that you'd get from craigslist.

8:28 Well, if you were writing up script or python code

8:31 Well, if you were writing up script or python code

8:34 that would pull that information

8:36 and then make sense out of it for you

8:39 and like write it to an actual excel file,

8:42 so like you want it to, you know, essentially do something

8:46 cool where it opens up a excel and writes to it

8:50 and you want it to like have,

8:53 you know, product,

8:55 price,

8:58 and condition, right,

9:00 and in product you'd want XBOX 360,

9:04 in price you'd want 150

9:07 and in condition you'd want new, right.

9:11 How would pull that data cleanly

9:13 so it doesn't put XBOX 360, 150, new all in one place?

9:17 so it doesn't put XBOX 360, 150, new all in one place?

9:21 Right?

9:21 So for that you need to be able to do string slicing, okay.

9:25 So for that you need to be able to do string slicing, okay.

9:28 So for instance, you might say,

9:31 console or product, right, or console product

9:35 is equal to, you know, find me the first pipe, right.

9:39 is equal to, you know, find me the first pipe, right.

9:42 So like let's say we have this guy here, there is a method

9:45 So like let's say we have this guy here, there is a method

9:48 you can do on data which is called index.

9:51 you can do on data which is called index.

9:54 So let's find the index of pipe.

10:00 It tells me that pipe is at index nine,

10:02 so zero, one, two, three, four,

10:06 five, six, seven, eight, nine.

10:10 Which means that if I index the string data

10:15 and I do nine, It'll give me back the pipe.

10:19 So what that tells me is that to get XBOX 360,

10:22 I need to go from the start of that string,

10:26 all the way up to the first pipe,

10:28 that should get me the product.

10:30 So I can say product is equal to data

10:33 from the start I don't need to say zero, right?

10:36 I can say from the start to

10:41 take the data dot index of the pipe.

10:48 Now if I show you product, boom.

10:51 Look at that, we got XBOX 360.

10:54 And so now I can write that product to the file,

10:57 teach you guys how to read and write to files.

10:59 Okay so then what tt will do is that it'll actually write

11:02 XBOX 360 in here for you, automagically.

11:06 Now, understand the power and implications,

11:09 "Well, why can't I just do that by hand?" Right?

11:11 That might be a question.

11:12 What if it was a hundred thousand products?

11:14 Can't do it by hand, good luck, right?

11:17 What if a million products that you were trying

11:19 to search from and find the best price

11:21 using your craigslist bot.

11:24 That's where programming comes in, okay?

11:26 It's very important to understand how programming in here

11:29 ties together and how it's more powerful

11:32 than you trying to do this on your own.

11:35 So that's really the power of string slicing, okay?

11:39 To break this line down for you a little bit more,

11:42 data, colon, that's just saying start from zero,

11:46 and then I'm saying data dot index of pipe,

11:49 what does that part evaluate to, right?

11:51 Dot index is a method that you can use on data, that's, okay

11:55 Dot index is a method that you can use on data, that's, okay

11:57 which just gives you something that's you're looking for,

12:00 you pass it into the function, right, that function

12:02 or that method index takes in one argument.

12:06 So what does this whole part evaluate to in our case?

12:09 This part evaluates to a nine.

12:12 Right, this whole part data do index, right?

12:16 Evaluates to nine, so if I do data zero through nine,

12:20 you can see that I just get XBOX 360, the same way.

12:23 Nine tells me up to but not including the pipe, okay.

12:26 Nine tells me up to but not including the pipe, okay.

12:29 So that's how we got the XBOX 360 part,

12:33 how would you get the 150?

12:35 You would kind of do the same way to get the price, okay.

12:39 You would kind of do the same way to get the price, okay.

12:40 But you would say instead of starting to look from here,

12:45 because then it'll find this pipe right here,

12:47 right, this one.

12:48 You wanna find this pipe now.

12:51 So you want to actually start your string search

12:55 from this location or this location that I'm highlighting.

12:59 And then you wanna search all the way up to this pipe

13:03 and stop there and get whatever

13:06 is in between these two pipes, okay?

13:09 So that's something I want you guys to try,

13:12 paste your code below for how you might get

13:15 the price and the condition, okay.

13:18 That would be a fun thing for you guys to do.

13:22 I'm gonna keep moving on

13:23 and talk a little bit more about string,

13:26 string slicing, cause there is still

13:28 a little bit more to it.

13:29 So

13:32 and again I want to keep practicing all this,

13:34 cause otherwise this is gonna overwhelm you,

13:37 all this knowledge.

13:40 So let's say that I have

13:44 I can do start, stop and here is another method

13:46 I can do start, stop and here is another method

13:48 to blow your mind, step.

13:50 So how many steps do wanna take when I'm reading something.

13:54 Okay.

13:55 So like let's say, I have this string,

13:59 "Hi how are you doing, it is very nice to meet you."

14:02 "Hi how are you doing, it is very nice to meet you."

14:07 I wanna make sure I'm consistent so not

14:10 double quote single quote and um

14:14 you know, let's just store that in some variable greeting.

14:17 Okay.

14:18 So if I say greeting so you can see the whole string,

14:22 so what I can do is take greeting, say start at zero,

14:28 stop at, I don't know, negative one,

14:31 so like all the way at the end,

14:33 and then I want you to.

14:37 Basically it'll go up to O, right?

14:40 Because it's up to like,

14:41 or sorry, it'll go up to the U part

14:44 but not this period right here.

14:47 Sorry.

14:48 I just, it won't let me highlight that period.

14:52 It won't go up to that period, okay.

14:54 It'll go up to U, because it's up to

14:56 but not including the period.

14:58 Sorry, I keep saying that a lot but I do know that

15:01 when you're starting to learn python or programming,

15:04 those things you'll forget a lot.

15:06 So I'm sorry if I'm reiterating

15:07 that fact over and over again.

15:09 Alright so we start from zero, we go up to the u, right?

15:11 Alright so we start from zero, we go up to the u, right?

15:14 We go from here all the way to here,

15:17 and then we wanna step, so instead of a,

15:23 for example, taking, I don't know,

15:26 reading every single one of these,

15:28 we can read every other letter.

15:30 So I can say instead of stepping by one which is default,

15:33 if I do one here you'll see,

15:36 this is pretty much the same string without the period.

15:39 And if I do nothing here, you'll see the same thing, right,

15:42 And if I do nothing here, you'll see the same thing, right,

15:44 cause that's a default.

15:46 And if I do step by two, this is basically saying

15:47 And if I do step by two, this is basically saying

15:50 give me every other letter.

15:54 Okay?

15:55 So think about maybe creating a game, gibberish game

15:59 that takes in a string as in a like it's a function

16:02 called gibberish, takes in a string and it just gibberi

16:05 called gibberish, takes in a string and it just gibberi

16:07 gibberish-izes it,

16:11 okay, it just like jumbles it up.

16:14 And if I do three here, you can see it stepping by three,

16:19 alright.

16:20 So pretty weird, but also cool at the same time,

16:23 So pretty weird, but also cool at the same time,

16:26 and

16:29 another thing that I wanna ask you guys as a practice to do

16:33 before I end this video is say I had the string hello,

16:40 I gave you the string, how would you reverse it?

16:45 Okay.

16:46 I want, if I gave you hello, I want back,

16:50 olleh.

16:52 Or if I gave you olleh,

16:57 you should give me back hello.

16:59 Okay.

17:01 So hopefully, that blew up your mind on string slicing,

17:04 So hopefully, that blew up your mind on string slicing,

17:06 I'll try to put some exercises on clever programmer dot com,

17:10 so if you haven't already go enroll in to the school

17:13 so if you haven't already go enroll in to the school

17:16 and there'll be exercises and extra resources

17:19 that you can use to get help.

17:21 Also there are others in the community who can help you.

17:24 Okay.

17:25 So good place to go for help,

17:27 but if you're watching on YouTube that's totally fine,

17:29 comment in the YouTube section below

17:31 and I will personally try to help you out,

17:34 other than that I'll see you guys in the next video.

react to story with heart
react to story with light
react to story with boat
react to story with money
L O A D I N G
. . . comments & more!