CS50 Video Player
    • 🧁

    • 🍬

    • 🍎

    • 🍿
    • 0:00:00[MUSIC PLAYING]
    • 0:00:05SPEAKER: Well, hello one and all and welcome to our short on string slicing.
    • 0:00:10I have here a program called phone.py that stores for me
    • 0:00:14a phone number as a string.
    • 0:00:17This is common for phone numbers because phone numbers might often
    • 0:00:20have other characters inside of them beyond numbers, things like dashes
    • 0:00:25or country codes, for instance.
    • 0:00:27So we'll use a string to store this phone number.
    • 0:00:32But there are certain interesting parts of a phone number
    • 0:00:35that I might want to use in my program.
    • 0:00:38So here, as you might know, in the US and other places too,
    • 0:00:42the first three digits of a phone number are known as the area code.
    • 0:00:46They can tell you where a caller is calling from.
    • 0:00:49Now, the last four digits might be useful too.
    • 0:00:53They could be used for security.
    • 0:00:55Let's say you're trying to verify someone's identity.
    • 0:00:58You could ask them in this case for the last four digits of their phone number.
    • 0:01:02So there are reasons here to try to get access to certain parts of this phone
    • 0:01:07number inside of this larger string.
    • 0:01:10Now, thanks to Python and thanks to string slicing,
    • 0:01:14we can actually get back certain substrings from our larger strings.
    • 0:01:19Let's see how we can do that.
    • 0:01:21Right now here I'm printing just the phone number.
    • 0:01:25I'll type Python of phone.py and I'll see, in this case, the entire phone
    • 0:01:28number just as it is.
    • 0:01:30That makes sense.
    • 0:01:32But let's say I want to find, in this case, just the first three digits.
    • 0:01:36I could do this.
    • 0:01:37I could say phone bracket and then inside of this bracket
    • 0:01:42I could tell Python which indices I want to get back from this larger string.
    • 0:01:48Here-- Let's check here-- this is the first index--
    • 0:01:520, then 1, then 2.
    • 0:01:55So I really want the first 0, 1, and 2 indices from this larger string
    • 0:02:02and make that my substring that I'll print out to the terminal.
    • 0:02:06So to get access to those values here, I can use this syntax.
    • 0:02:11I could say I want to start at the zeroth index
    • 0:02:14and I want to go until I hit the third index exclusive.
    • 0:02:19And so this is a feature of Python string slicing.
    • 0:02:23The first number you include will be inclusive.
    • 0:02:27I will include the character at this index.
    • 0:02:30The second number you include will be exclusive.
    • 0:02:35I'll go up to but not including the character at this index.
    • 0:02:40So if I now run Python of phone.py, well, I'll
    • 0:02:44now see 617, the first three digits of this phone number.
    • 0:02:49I could even simplify this a little bit more.
    • 0:02:52Instead of specifying that I want the zeroth index here,
    • 0:02:56I could actually just leave this blank.
    • 0:02:59If I leave it blank, Python will assume I
    • 0:03:01want to start at the beginning of this string
    • 0:03:04and go up to but not including whatever character is at this index here.
    • 0:03:09So I'll say Python of phone.py and I'll again get back 617.
    • 0:03:16But now what if we want the last four digits?
    • 0:03:20How could we get those?
    • 0:03:21Well, if I do this one--
    • 0:03:23let's count up here 0, 1, 2, 3, 4, 5, 6, 7, 8.
    • 0:03:29In this case, I want the character or digit at the eighth index
    • 0:03:35all the way up to 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11--
    • 0:03:43so really up to 12 exclusive-- so 8, 9, 10, 11, those last four digits.
    • 0:03:51If I run Python of phone.py, I will see those last four digits.
    • 0:03:57But this could be really tedious.
    • 0:03:59I mean, you don't really want to be looking at your strings
    • 0:04:01counting individual characters.
    • 0:04:04So there are other ways to do this.
    • 0:04:06One simplification here might be similar to what we just did.
    • 0:04:09I could remove a value for this second number here
    • 0:04:13and Python will assume I want to go from the character at the eighth index
    • 0:04:19all the way to the end of my string.
    • 0:04:22So I could say Python of phone.py and that
    • 0:04:24will give me back the full four digits here, similar to using in this case 12.
    • 0:04:30But I'd argue this is still not the best design because what
    • 0:04:35if my phone number changes.
    • 0:04:37I could type plus 1 here and I could do Python of phone.py and now my program is
    • 0:04:44broken because I added two more characters-- really three--
    • 0:04:48at the beginning.
    • 0:04:50So thankfully there is a way to solve for this problem.
    • 0:04:55If the characters you want are always at the end of your string,
    • 0:04:59you can get them using a special kind of indexing
    • 0:05:02that Python gives you access to.
    • 0:05:05I'm going to type here negative 4, which seems a little bit strange,
    • 0:05:11but let's visualize it.
    • 0:05:12So, again, in our string, this is index 0.
    • 0:05:16But instead of going forward here, let's actually work backwards-- again,
    • 0:05:22zero-th index--
    • 0:05:23if I go back around to the end of my string, this, according to Python,
    • 0:05:29is index negative 1 and this is index negative 2, index negative 3
    • 0:05:36and index negative 4.
    • 0:05:39Hmm, so it seems like I've gone back around on my string four characters
    • 0:05:45and I'm now asking for everything until the end of my string,
    • 0:05:50thanks to this colon with no particular number at the end of it.
    • 0:05:54Let's try this.
    • 0:05:56I'll run Python of phone.py and now I'll see those same four digits, 1000.
    • 0:06:03So thanks to string slicing, we can take these longer strings
    • 0:06:08and make substrings.
    • 0:06:10You've seen how to do that in a variety of ways.
    • 0:06:13This then was our short on string slicing and we'll see you next time.
  • CS50.ai
Shortcuts
Before using a shortcut, click at least once on the video itself (to give it "focus") after closing this window.
Play/Pause spacebar or k
Rewind 10 seconds left arrow or j
Fast forward 10 seconds right arrow or l
Previous frame (while paused) ,
Next frame (while paused) .
Decrease playback rate <
Increase playback rate >
Toggle captions on/off c
Toggle mute m
Toggle full screen f or double-click video