String Slicing: BEHIND THE SCENES!

Hi! Welcome to this section! We are going to discuss String Slicing.
String slicing is a way to extract a portion of a string, a subset of a string using indices like we leaned on the previous post on String indexing.
But… there is a different syntax that I must present

Let’s get started!




In the slide below you can see the syntax required to extract a string slice.
  • First, we assign the slice to a variable (this is not necessary, but if you may want to use it subsequent steps in your code, you must store it in a variable)
  • Then, we write the name of the variable that holds the original string. In this case, a.
  • And we see a new syntax we haven’t discussed. [start: end: step]



Let's look at different alternatives for the parameters inside [ ]:
  • If you just set one number inside the [ ] e.g 'hi'[ 1 ] that number will correspond to the index of a character in the grid, and you will get the single character located at that index.
  • If you set a single number but you put a colon (:) before the number, like this: [ : (number)] e.g [:5] you will get a slice of the string that starts from index 0 (this is the default value for the starting index) up to but not including the character located at the index you set as the final index.
  • If you set two numbers separated by a colon, like this [ (number1) : (number2) ] e.g [3:5] you will get a slice of the string that starts from 3 and contains all the characters up to but not including the character located at the end index.
  • If you set three numbers separated by colons, like this [ (number1) : (number2) : (number3) ] e.g [1:8:2] you will get a slice that starts from the character located at index 1 up to but not including the character located at index 8. And the third parameter determines that characters every two indices will be included. It will jump from index 1 to index 3 and from index 3 to index 5 and so on, without including characters located in between.
  • If you set -1 as the only parameter inside the [ ] , you will get the last character in the string, independent of its length.
  • If you set a single number inside [ ] followed by a colon (:) like this: [(number1) : ] e.g [2: ] it will give you a slice of the string that starts at the index you set as the start, and will include every character up to the end of the string.
  • If you include two colons (:) followed by a number, like this: [::(number1)] that number will be the step ("jump" from one index to the next) e.g [::2].
  • If you only set a single colon (:) inside the [ ], you will get a "copy" of the string. In the following weeks, you will learn why this is an important feature and how it can help you in actual code.

Here are some examples of how string slicing works behind the scenes.




Here you can see the result of these examples executed in python's shell:


Here we have an example of a substring:


Now that we’ve seen how String Slicing works, we have analyze alternative syntax for its parameters:

TIME TO PRACTICE! 



Here are some exercises for you to solve in a piece of paper or in your notebook. I recommend that you solve them manually, not in Python’s shell so you have a firm grasp of how parameters work and alternative syntax:

a[5:10]
a[2:12:2]
a[6:11:5]
a[10:10:2]
a[:5]
a[11:]
a[::3]
a[2::3]
a[:8:4]
You can check your answers by executing these examples in Python’s shell

Hope it help! 
If you have any questions, please post them on the forums or right below this post, your classmates and Community TAs will always be there to help you!
Estefania.

Comments

  1. Any idea why this is happening to me? Very discouraging when the most basic thing doesn't seem to work....

    a
    Out[2]: 'i_love_coding'

    a(5:10)
    File "", line 1
    a(5:10)
    ^
    SyntaxError: invalid syntax




    a(5:10)
    File "", line 1
    a(5:10)
    ^
    SyntaxError: invalid syntax

    ReplyDelete

Post a Comment