Accel Thinking

Python Learning Notes

Understand Python String Slicing in One Minute

A beginner-friendly Python note on word[1:4], include start, exclude stop, and slicing boundaries.

After learning string indexes, many beginners run into the next question: what characters does word[1:4] actually take? Why does it use 4, but not include the character at index 4?

This note focuses on the most important slicing rule: include the start, exclude the stop.

Common Mistake

Suppose we have this string:

word = "PYTHON"
print(word[1:4])

Many students expect word[1:4] to take indexes 1 through 4, which would be YTHO.

But the actual output is:

YTH

The reason is that the second number, 4, is the stopping position. It is not the last index included in the slice.

Core Idea

First, write the indexes under the characters:

P  Y  T  H  O  N
0  1  2  3  4  5

word[1:4] means:

So the result is:

YTH

This is the slicing rule: include start, exclude stop.

When reading a slice, do not read it as “from the first to the fourth.” A better way is:

word[start:stop]

Start at start, and stop right before stop.

For example:

word = "PYTHON"

print(word[0:2])
print(word[2:6])

The output is:

PY
THON

word[0:2] takes indexes 0 and 1, but not index 2. word[2:6] starts at index 2 and reaches the end of the string, because index 6 is the position right after the last character.

Why Is stop Excluded?

For beginners, a useful way to think about stop is this: it is like a boundary line where the slice stops.

P | Y  T  H | O  N
0   1  2  3   4  5

word[1:4] starts at the boundary before index 1 and stops at the boundary before index 4. The characters between those boundaries are YTH.

This design also makes the length easy to calculate.

word = "PYTHON"
part = word[1:4]
print(len(part))
3

Because 4 - 1 = 3, word[1:4] contains 3 characters.

Comparison

Code Output How to read it
word[1] Y Get one character at index 1
word[1:4] YTH Start at 1, stop before 4
word[0:3] PYT Take 3 characters from the beginning

Why This Matters

String slicing is common in Python exercises and CCC-style problems. Whenever you need a piece of text, a substring, or a palindrome check, slicing can help.

For example:

text = "level"
print(text[1:4])

Output:

eve

Later, when solving palindrome problems, you will often take the middle part of a string. Understanding “include start, exclude stop” helps you avoid many boundary mistakes.

Practice

  1. If word = "CODING" , what does word[1:4] output?
  2. If word = "CODING" , what does word[0:3] output?
  3. Challenge: use slicing to get "HON" from "PYTHON" .

One-Sentence Takeaway

String slicing includes start and excludes stop; for word[1:4], take indexes 1, 2, and 3, but not 4.

Once slicing feels natural, substrings, palindromes, and many CCC string problems become much easier.