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

Adjacent literal strings are concatenated by the Python compiler into a single string, which, although at first it seems rather pointless, can be useful for a nicely formatted code, as we'll see later.

相邻的字符串自动合并

>>> 'sss' 'ddd'
'sssddd'
>>> p = 'sss' 'ddd'
>>> print(p)
sssddd
>>>

If you want a literal string containing newlines, you have two options, use multiline strings or use escape sequences.
First, let's look at multiline strings. Multiline strings are delimited by three quote characters rather than one. Here's an example using three double quotes. Notice how, when the string is echoed back to us, the newlines are represented by the \n escape sequence. We can also use three single quotes.

要想实现多行字符串,可以使用三引号(单双都可以,结构化体现,所见即所得,自动添加\n)

 

>>> """This is
... a multiline
... string"""
'This is\na multiline\nstring'
>>> '''So
... is
... this.'''
'So\nis\nthis.'
>>>

As an alternative to use multiline quoting, we can just embed the control characters ourselves. To get a better sense of what we're representing, we can use print to see the string.

要想实现多行字符串,还可以手动添加\n

>>> m = 'This string\nspans mlutiple\nlines.'
>>> m
'This string\nspans mlutiple\nlines.'
>>> print(m)
This string
spans mlutiple
lines.
>>>

If you're working on Windows, you might be thinking that newlines should be represented by the carriage return and newline couplet \r\n. There's no need to do that with Python. Since Python 3 has a feature called Universal Newline Support, which translates from the simple \n to the native newline sequence for your platform on input and output. You can read more about Universal Newline Support in PEP 278. 

Windows上的换行符是\r\n,Linux/Mac上的换行符是/r

不用担心平台问题,在你的代码里,你就统一用\n就行了,python会根据代码运行时所在的平台,自动进行转换

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

 

 

We can use the escape sequences for other purposes, too, such as incorporating tabs with \t or allowing us to quote characters within strings by using \ double quote or \ single quote. See how Python is smarter than we are at using , although Python will also resort to escape sequences when we use both types of quotes in a string. Because backslash has special meaning, to place a backslash in a string, we escape the backslash with itself. To reassure ourselves that there really is only one backslash in that string, we can print it. You can read more about escape sequences in the Python documentation at python.org. 

\n 回车
\t 制表符
其他组合

本身

\" -> "

\' -> '

\\ -> \

 

 

 

 

 

 

 

Sometimes, particularly when dealing with strings such as Windows file system paths or regular expression patterns, which use backslashes extensively, the requirement to double up on backslashes can be ugly and error prone. Python comes to the rescue with its raw strings. Raw strings don't support any escape sequences and are very much what you see is what you get. To create a raw string, prefix the opening quote with a lowercase r. 

 在处理Windows路径的时候,使用r将其中所有的反斜线(默认特异功能)都转义为本身,否则,你需要这样:

path = 'C:\\Users\\Merlin\\Documents\\Spells'

麻烦,丑陋,也不好复制粘贴

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

We can use the string constructor to create string representations of other types such as integers or floats. 

构造一个字符串的时候,还可以使用str()函数,将某些其他的数据类型直接转换过来,例如整数和浮点数

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

 

Strings in Python are what are called sequence types, which means they support certain common operations for querying sequences. For example, we can access individual characters using square brackets with an integer 0‑based index. Note that in contrast to many programming languages, there is no separate character type distinct from the string type. The indexing operation we just used returns a full‑blown string that contains a single character element, something we can test using Python's built‑in type function. There will be more on types and classes later in the course. 

在Python中,字符串也是一种序列,其中的每个字母都有索引

通过索引取出来的字符的类型也是字符串

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

 

String objects also support a wide variety of operations implemented as methods. We can list those methods using help on the string type. Ignore all the hieroglyphics with underscores for now and page down until you see the documentation for the capitalized method. Press Q to quit the help browser, and we'll try to use that method. 

First, let's make a string that deserves capitalization, the proper noun of a capital city, no less. To call methods on objects in Python, we use the dot after the object name and before the method name. Methods are functions, so we must use the parentheses to indicate that the method should be called. Remember that strings are immutable, so the capitalized method didn't modify c in place, rather, it returned a new string. We can verify this by displaying c, which remains unchanged. You might like to spend a little time familiarizing yourself with the various useful methods provided by the string type.

我们可以使用很多内置函数来对一个字符串进行各种操作

例如,可以使用capitalize()函数实现首字母大写,此操作不会覆盖原有的值

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

 

Finally, because strings are fully Unicode capable, we can use them with international characters easily, even in literals because the default source code encoding for Python 3 is UTF‑8. For example, if you have access to Norwegian characters, you can simply enter this. Alternatively, you can use the hexidecimal representations of Unicode code points as an escape sequence prefixed by \u, which I'm sure you'll agree, is somewhat more unwieldy. Similarly, you can use the \x escape sequence, followed by a two‑character hexidecimal string or an escaped octal string to include Unicode characters in a string literal. There are no such Unicode capabilities in the otherwise similar bytes type, which we'll look at next.

Python3中,默认的字符编码格式就是UTF-8(默认就支持中文)

不过它也支持Unicode,例如

\u00e5 - 十六进制的00e5,代表挪威字母a

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

 

 

 

adjacent 相邻的
concatenate 连接
pointless 无意义的
carriage return 回车
escape sequence 转义序列,\n, \t,等等
delimiter 分隔符
backslash 反斜线,\
Note that 记住,请注意

 

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

 

上一篇:[LeetCode] 583. Delete Operation for Two Strings


下一篇:类thinkpad笔记本安装deepinv20后启动黒屏的解决