Core Python | 2 - Core Python: Getting Started | 2.4 - Introducing Strings, Collections, and Iterati

Strings in Python have the data type str, spelled s‑t‑r, and we've been using them extensively already. Strings are sequences of Unicode code points, and for the most part, you can think of code points as being like characters, although they are not strictly equivalent. The sequence of characters in a Python string is immutable, meaning that once you've constructed a string, you can't modify its contents. Literal strings in Python are delimited by quotes, you could use single quotes or double quotes. You must, however, be consistent. For example, you can't use single quotes on one side and double on the other, like this. Supporting both quoting styles allows you to easily incorporate the other quote character into the literal string without resorting to ugly escape character. Notice that the REPL exploits the same quoting flexibility when echoing the strings back to us. Beautiful text strings rendered in literal form, simple elegance. At first sight, support for both quoting styles seems to violate an important principle of Pythonic style from the Zen of Python. There should be one, and preferably only one, obvious way to do it. In this case, however, another aphorism from the same source, practicality beats purity, takes precedence. The utility of supporting two quoting styles is valued more highly than the alternative, a single quoting style combined with more frequent use of ugly escape sequences, which we'll encounter shortly.

Core Python | 2 - Core Python: Getting Started | 2.4 - Introducing Strings, Collections, and Iterati

 

extensively 广泛地
equivalent 相等地
immutable 不可变的
delimit 界定
quote 引号
escape character 转义字符
literal 文字的,字面的
literal form 字面形式
take precedence 优先
rendered in literal form 以字面形式呈现
simple elegance 简单的优雅
at first sight 乍一看
violate an important principle 违反了重要的原则
preferably only one 只有一个更好/最好也只有一个
aphorism 格言
practicality beats purity 实用大于纯粹
we'll encounter shortly 我们很快就会遇到
alternative 二选一
上一篇:vue 组件之间的通信


下一篇:javascript字面量