Understand Python String Slicing in One Minute



Start NorthStar

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.

Python 学习笔记

一分钟看懂字符串切片

一篇适合初学者的 Python 笔记,讲清楚 word[1:4]、包含 start、不包含 stop,以及切片边界。

Share this article

Copy the link, open your phone share sheet, scan the QR code for WeChat, or send by email.




Email

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:

  • start at index 1, taking Y
  • continue with index 2, taking T
  • continue with index 3, taking H
  • stop at index 4, but do not take O

So the result is:

YTH

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

Recommended Pattern

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.

学完字符串下标以后,很多同学马上会遇到下一个问题:word[1:4] 到底取了哪几个字符?为什么看起来写了 4,结果却不包含下标 4 的字符?

这篇只讲字符串切片里最重要的一条规则:包含开始,不包含结束。也就是常说的“左闭右开”。

常见误解

假设有这个字符串:

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

很多同学会以为 word[1:4] 会取下标 14,也就是 YTHO

但实际输出是:

YTH

原因是切片里的第二个数字 4 是停止位置,不是最后一个被取到的位置。

核心概念

先把下标标出来:

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

word[1:4] 的意思是:

  • 从下标 1 开始,取到 Y
  • 继续取下标 2T
  • 继续取下标 3H
  • 到下标 4 停下,但不取 O

所以结果是:

YTH

这就是切片规则:包含 start,不包含 stop。

推荐写法

读切片时,不要把它读成“第 1 个到第 4 个”。更好的读法是:

word[start:stop]

start 开始取,到 stop 前面停下。

例如:

word = "PYTHON"

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

输出是:

PY
THON

word[0:2] 取下标 01,不取下标 2word[2:6] 从下标 2 开始,一直取到字符串末尾,因为下标 6 正好是“末尾后面的位置”。

为什么 stop 不包含?

对初学者来说,最实用的理解是:stop 像一条分割线,切片在这条线前面停下。

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

word[1:4] 就是从下标 1 前面的线开始,到下标 4 前面的线结束。中间夹住的是 YTH

这样设计还有一个好处:切片长度很好算。

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

因为 4 - 1 = 3,所以 word[1:4] 取到 3 个字符。

对比一下

写法 输出 怎么读
word[1] Y 取下标 1 的一个字符
word[1:4] YTH 从 1 开始,到 4 前停下
word[0:3] PYT 从开头取 3 个字符

为什么这个概念很重要?

字符串切片在 Python 练习和 CCC 题里很常见。只要题目需要取一段文字、检查 substring、做回文判断,切片都会很有用。

比如:

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

输出:

eve

后面做回文题时,你会经常需要取字符串中间的一段。理解“左闭右开”,可以少掉很多边界错误。

小练习

  1. word = "CODING"word[1:4] 输出什么?
  2. word = "CODING"word[0:3] 输出什么?
  3. 挑战题:用切片从 "PYTHON" 中取出 "HON"

一句话记住

字符串切片包含 start,不包含 stop;看到 word[1:4],就取 1、2、3,不取 4。

理解切片以后,你就能更顺地进入 substring、回文和更多 CCC 字符串题。

Questions or feedback?

Have a question about this article or want to suggest an improvement? Send us a private message.

Send Feedback

有问题或建议?

如果你对本文有疑问,或希望提出改进建议,请给我们发送私密留言。

发送反馈

Back to Python Learning Notes
返回 Python 学习笔记

滚动至顶部