Python for Absolute Beginners Series #9

In this video, we introduce the concept of string slicing.

We know that a string is a line of code, starting with a letter, that is surrounded in either single or double quotes.

The quotes must be consistent, you can’t have a line of code starting with a single quote and ending in a double quote.

String slicing allows you to take the code and divide it. If you have a list of 100 emails and you want to separate the username and what domain it uses e.g. Gmail, yahoo, etc. string slicing comes in handy.

In order to slice strings, you’ll need to get familiar with indexing.

Indexing basically tells you where to grab data in the list.

Indexing starts at 0. So for ‘hello’ the first number is 0.

See:

h = (0)

e = (1)

l = (2)

l = (3)

o = (4)

This is how the indexing function works: [START:STOP]

If you were to put in an index [0:4], you would get ‘hell’ and not ‘hello.’

The strings goes up to 4 but doesn’t include 4.

We’ve just brushed the surface of string slicing, we’ll dive deeper in the next video.

 

Leave a comment