0Pricing
NLP Academy · 课时

字符串、字符与编码

了解文本如何存储为字节与 Unicode

字符串、字符与编码 是 CoddyKit 上的免费 NLP Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 NLP Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 NLP Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Text Is Made of Characters

Every piece of text you process is a string: an ordered run of characters like letters, digits, spaces, and symbols. 🔤

Strings in Python

In Python a string is wrapped in quotes. You can store a whole sentence in a single variable and work with it as one value.

text = "NLP turns text into insight"
print(type(text))  # <class 'str'>

Each Character Has a Position

A string is indexed, so each character sits at a numbered position starting from zero. That lets you reach any single letter.

word = "data"
print(word[0])  # d
print(word[3])  # a

Computers Store Numbers

A computer cannot store the letter A directly. It stores a number, and an encoding is the agreed map between characters and those numbers.

ASCII Came First

ASCII mapped 128 basic English characters to numbers. It worked for plain English but had no room for accents or other alphabets.

Unicode Covers Everything

Unicode gives every character a unique code point, from English letters to Chinese script and emoji. Modern Python strings are Unicode.

Code Points in Python

The ord function shows a character's Unicode number, and chr turns a number back into its character.

print(ord('A'))   # 65
print(chr(233))   # é

Encoding Turns Text Into Bytes

To save or send text, Python turns it into bytes using a scheme like UTF-8. Bytes are what files and networks actually carry.

data = "café".encode("utf-8")
print(data)  # b'caf\xc3\xa9'

UTF-8 Is the Default Choice

UTF-8 stores common characters in one byte and rarer ones in more. It is compact, universal, and the standard for almost all text today.

Decoding Bytes Back to Text

Reading raw bytes means decoding them with the same scheme used to encode. The wrong scheme produces garbled or broken characters.

raw = b'caf\xc3\xa9'
print(raw.decode("utf-8"))  # café

Encoding Mistakes Cause Mojibake

Decoding with the wrong encoding gives mojibake, that jumble of odd symbols. Always know and match your encoding to avoid it.

Quick Check

Let us check the difference between two key text standards.

Recap: Text as Encoded Strings

You saw that text is a string of characters, mapped to numbers by Unicode and stored as UTF-8 bytes. Match encodings and your text stays clean. ✅

常见问题解答

「字符串、字符与编码」课时是免费的吗?

是的 — 「字符串、字符与编码」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 NLP Academy 课程的其余内容,请升级到 CoddyKit PRO。 NLP Academy 课程共包含 4 节课。

「字符串、字符与编码」这节课中我会学到什么?

了解文本如何存储为字节与 Unicode 你通过在浏览器中直接运行的动手代码来练习 NLP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 NLP Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 NLP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。

「字符串、字符与编码」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 NLP Academy 课中编写并运行代码吗?

能。每节 NLP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 字符串、字符与编码
  2. 将文本文件读入 Python
  3. 统计词语与字符
  4. 构建第一个词频表
← 返回 NLP Academy