Loading...

Python Slicing

The end number is the first place you do not take. guest holds lida. The places are 0, 1, 2, and 3: l, i, d, a. Counting starts at 0, not at 1. guest[0:2] takes places 0 and 1, so the output is li. Place 2, the d, stays out.

The end number stays out

python
guest = "lida"
print(guest[0:2])
print(guest[1:])

The first line is li. The second line is ida. guest[1:] means from place 1 through the end, so i, d, and a. There is no end number to leave out. guest[:2] is the same cut as guest[0:2]: from the start up to, but not including, place 2.

One character, from the start or the end

guest[0] is the first character, l. guest[-1] is the last character, a. The minus sign counts backward from the end. -1 is the last place. It is not a subtraction you write out.

First character, then last

python
guest = "lida"
print(guest[0])
print(guest[-1])

The two lines are l, then a. These places belong to the word lida.

📊 Places in lida

Place Character
0 l
1 i
2 d
3 a

A place that is not there stops the program

lida has places 0 through 3. guest[4] asks for a place that does not exist, so the program stops. The length is 4, and the last place is 3, not 4. Length and place are not the same number.

The same brackets, a whole item

A list is several items in order, written in square brackets and separated by commas. names = ["lida", "tea"] has two items. names[0] is the whole first item, lida, not the letter l. names[1] is tea. A string cut gives characters. A list cut gives items. Building and changing lists is a later page. Here you only take an item by its place.

First item, then second

python
names = ["lida", "tea"]
print(names[0])
print(names[1])

Left for the next page

This page does not decide true or false, does not place a name inside a sentence, and does not change case. The next page is a value that is true or false. The cut stays a way to take characters, or a whole item, by place.

Check yourself

  1. guest[0] is l. guest[-1] is a.
  2. guest[0:2] is li, not lid.
  3. guest[1:] is ida.
  4. guest[4] stops the program.
  5. names[0] is the word lida, not the letter l.

Misses

  • Counting the first character as place 1
  • Expecting lid from guest[0:2]
  • Expecting the letter l from names[0]
  • Treating the length 4 as a place you can read
  • Reading -1 as subtraction instead of the last character

Keep

  • Places start at 0. guest[0] is l. guest[-1] is a.
  • guest[0:2] is li. The end number is not taken.
  • guest[1:] is ida.
  • guest[4] stops. The last place in lida is 3.
  • names[0] is the whole first item. The next page is true or false.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
5
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top