Menu
Coddy logo textTech

What is slicing?

Lesson 12 of 19 in Coddy's Into the Past || Complete Beginner for Python Strings course.

Slicing a String

In python slicing is an interesting task. Let's take a look:

name = "SAMUEL"

Think about it like a loaf of bread.  You can grab a piece, part or the whole thing.  

Let's look at how to do this.  Python is zero index bases meaning it starts counting at zero under the letter "S"

Next how it work [start:stop:step]

  • start: where to start and it is inclusive; meaning it is included
  • stop: where to end but it is exclusive; meaning it stops right before the number
  • step: at witch interval to move through the string
  • also a single number is only that character

Examples:

  • name[2] --> "M"
  • name[1:4] --> "AMU"
  • name[1:5:2] --> "AU"
challenge icon

Challenge

Easy

Now run the code that is given to you in the code editor and see for yourself

Try it yourself

def slice(name):
    return name[0:4]

All lessons in Into the Past || Complete Beginner for Python Strings