Skip to Content
Python入门指南, 3rd Edition
book

Python入门指南, 3rd Edition

by Bill Lubanovic
September 2025
Intermediate to advanced
660 pages
7h 15m
Chinese
O'Reilly Media, Inc.
Content preview from Python入门指南, 3rd Edition

第 4 章 弦乐 字符串

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

我总是喜欢奇怪的字符。

蒂姆-伯顿

计算机 书籍通常给人的印象是编程就是数学。实际上,大多数程序员处理文本字符串的频率要高于处理数字的频率。逻辑思维(和创造性思维!)通常比数学技能更重要。

字符串是我们在 Python 序列的第一个例子。序列是元素的有序集合。 您可以从任何序列中获得以下一些信息:

  • 元素是否在序列中

  • 元素值的索引

  • 特定索引中的元素

  • 给定范围内元素的切片

  • 序列的长度

  • 元素的最小值和最大值

在接下来的章节中,您还将看到其他序列,包括元组和列表(第 8 章) 以及字节和字节数组(第 5 章)。

Python 字符串是由 个字符组成的序列。但什么是字符呢? 它是书写系统中最小的单位,包括字母、数字、符号、标点符号,甚至是空白或指令,如换行。 一个字符是由它的意义(如何使用)定义的,而不是由它的外观定义的。 它可以有多个可视化表示(在不同字体中),而且可以有多个字符具有相同的外观(例如可视化H ,在拉丁字母中表示H 音,但在西里尔字母中表示拉丁文N 音)。

本章使用 ASCII(基本字符集)示例,集中介绍简单文本字符串的制作和格式化。 两个重要的文本主题将推迟到第 17 章:Unicode 字符(如我刚才提到的HN 问题)和正则表达式(模式匹配)。

与其它语言不同,Python 中的字符串是不可变的。 您不能就地改变一个字符串,但您可以将字符串的一部分复制到一个新字符串中,以获得相同的效果。 我们很快就会看到如何做到这一点。

在 Python 中,单个字符是不独立存在的,它们总是字符串的一部分。 字符串可以有一个字符,也可以有一百万个字符,甚至一个字符都没有(空字符串)。

用引号创建

通过用匹配的单引号或双 引号括住字符来创建 Python 字符串:

>>> 'Snap'
'Snap'
>>> "Crackle"
'Crackle'
>>> 'Flop'
'Flop'

交互式解释器会回声带有单引号的字符串,但 Python 对所有字符串的处理是完全一样的。

Python 有一些特殊类型的字符串,用第一个引号前的字母表示:

  • fF 启动 一个f 字符串,用于格式化,本章末将对其进行描述。

  • rR 开头的 是一个原始字符串,用于防止字符串中出现转义序列(参见"Escape with\"第 19 章,了解其在字符串模式匹配中的用途)。

  • 此外,还有fr (或FR,Fr, 或fR )组合,用于启动原始 f 字符串。

  • u 开头的是 Unicode 字符串,与普通字符串相同。

  • b 的起始值类型为bytes (第 5 章)。

除非我提到这些特殊类型之一,否则我总是在谈论普通的 Python 文本字符串。 字符串中的每个字符都可以是 Unicode 标准中定义的任何字符。 同样,我将在第 17 章才对 Unicode 进行详细讨论,但现在要知道的要点是,Unicode 比 ASCII 要大得多

为什么有两种引号字符? 主要目的是创建包含引号字符的字符串。 可以在双引号字符串中使用单引号,也可以在单引号字符串中使用双引号:

>>> "'Nay!' said the naysayer. 'Neigh?' said the horse."
"'Nay!' said the naysayer. 'Neigh?' said the horse."
>>> 
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

生成式人工智能设计模式

生成式人工智能设计模式

Valliappa Lakshmanan, Hannes Hapke

Publisher Resources

ISBN: 9798341668898