Python-字符串详解(包含长字符串和原始字符串)(Python string explanation (including long string and original string))

一、什么是字符串

若干个字符的集合就是一个字符串(String)。Python 中的字符串必须由双引号或者单引号包围,具体格式为:”字符串内容”,’字符串内容’
字符串的内容可以包含字母、标点、特殊符号、中文、日文等全世界的所有文字
合法的字符串:”123789″、”123abc”、”http://c.biancheng.net/python/”、”C语言中文网成立8年了”
Python 字符串中的双引号和单引号没有任何区别

若干个字符的集合就是一个字符串(String)。Python 中的字符串必须由双引号或者单引号包围,具体格式为:”字符串内容”,’字符串内容’

" "
' '

字符串的内容可以包含字母、标点、特殊符号、中文、日文等全世界的所有文字

合法的字符串:”123789″、”123abc”、”http://c.biancheng.net/python/”、”C语言中文网成立8年了”

Python 字符串中的双引号和单引号没有任何区别

二、处理字符串中的引号

1) 对引号进行转义
在引号前面添加反斜杠就可以对引号进行转义,让 Python 把它作为普通文本对待,例如:

str1 = ‘I\’m a great coder!’
str2 = “引文双引号是\”,中文双引号是“”
print(str1)
print(str2)

运行结果:
I’m a great coder!引文双引号是”,中文双引号是“
2) 使用不同的引号包围字符串

如果字符串内容中出现了单引号,那么我们可以使用双引号包围字符串,反之亦然。例如:

str1 = “I’m a great coder!” #使用双引号包围含有单引号的字符串
str2 = ‘引文双引号是”,中文双引号是“’ #使用单引号包围含有双引号的字符串
print(str1)
print(str2)

运行结果和上面相同。

1) 对引号进行转义

在引号前面添加反斜杠就可以对引号进行转义,让 Python 把它作为普通文本对待,例如:

\
  • str1 = ‘I\’m a great coder!’
  • str2 = “引文双引号是\”,中文双引号是“”
  • print(str1)
  • print(str2)

运行结果:

I’m a great coder!引文双引号是”,中文双引号是“

2) 使用不同的引号包围字符串

如果字符串内容中出现了单引号,那么我们可以使用双引号包围字符串,反之亦然。例如:

  • str1 = “I’m a great coder!” #使用双引号包围含有单引号的字符串
  • str2 = ‘引文双引号是”,中文双引号是“’ #使用单引号包围含有双引号的字符串
  • print(str1)
  • print(str2)

运行结果和上面相同。

三、字符串的换行

 Python 不是格式自由的语言,它对程序的换行、缩进都有严格的语法要求。要想换行书写一个比较长的字符串,必须在行尾添加反斜杠,请看下面的例子:

s2 = ‘It took me six months to write this Python tutorial. \
Please give me more support. \
I will keep it updated.’

上面 s2 字符串的比较长,所以使用了转义字符对字符串内容进行了换行,这样就可以把一个长字符串写成多行。
另外,Python 也支持表达式的换行,例如:

num = 20 + 3 / 4 + \
2 * 3
print(num)

 Python 不是格式自由的语言,它对程序的换行、缩进都有严格的语法要求。要想换行书写一个比较长的字符串,必须在行尾添加反斜杠,请看下面的例子:

\
  • s2 = ‘It took me six months to write this Python tutorial. \
  • Please give me more support. \
  • I will keep it updated.’

上面 s2 字符串的比较长,所以使用了转义字符对字符串内容进行了换行,这样就可以把一个长字符串写成多行。

\

另外,Python 也支持表达式的换行,例如:

  • num = 20 + 3 / 4 + \
  • 2 * 3
  • print(num)

四、Python长字符串

使用三个单引号或者双引号可以对多行内容进行注释,这其实是 Python 长字符串的写法。所谓长字符串,就是可以直接换行(不用加反斜杠)书写的字符串。Python 长字符串由三个双引号或者三个单引号包围,语法格式如下:
“””长字符串内容””””’长字符串内容”’

在长字符串中放置单引号或者双引号不会导致解析错误。如果长字符串没有赋值给任何变量,那么这个长字符串就不会起到任何作用,和一段普通的文本无异,相当于被注释掉了。
注意,此时 Python 解释器并不会忽略长字符串,也会按照语法解析,只是长字符串起不到实际作用而已。

当程序中有大段文本内容需要定义成字符串时,优先推荐使用长字符串形式,因为这种形式非常强大,可以在字符串中放置任何内容,包括单引号和双引号。【实例】将长字符串赋值给变量:

longstr = ”’It took me 6 months to write this Python tutorial.
Please give me a to ‘thumb’ to keep it updated.
The Python tutorial is available at http://c.biancheng.net/python/.”’
print(longstr)

长字符串中的换行、空格、缩进等空白符都会原样输出,所以你不能写成下面的样子:

longstr = ”’
It took me 6 months to write this Python tutorial.
Please give me a to ‘thumb’ to keep it updated.
The Python tutorial is available at http://c.biancheng.net/python/.
”’
print(longstr)

虽然这样写格式优美,但是输出结果将变成:
    It took me 6 months to write this Python tutorial.    Please give me a to ‘thumb’ to keep it updated.    The Python tutorial is available at http://c.biancheng.net/python/. 

字符串内容前后多出了两个空行,并且每一行的前面会多出四个空格。

使用三个单引号或者双引号可以对多行内容进行注释,这其实是 Python 长字符串的写法。所谓长字符串,就是可以直接换行(不用加反斜杠)书写的字符串。Python 长字符串由三个双引号或者三个单引号包围,语法格式如下:

\
"""
'''

“””长字符串内容””””’长字符串内容”’

在长字符串中放置单引号或者双引号不会导致解析错误。如果长字符串没有赋值给任何变量,那么这个长字符串就不会起到任何作用,和一段普通的文本无异,相当于被注释掉了。

注意,此时 Python 解释器并不会忽略长字符串,也会按照语法解析,只是长字符串起不到实际作用而已。

当程序中有大段文本内容需要定义成字符串时,优先推荐使用长字符串形式,因为这种形式非常强大,可以在字符串中放置任何内容,包括单引号和双引号。【实例】将长字符串赋值给变量:

  • longstr = ”’It took me 6 months to write this Python tutorial.
  • Please give me a to ‘thumb’ to keep it updated.
  • The Python tutorial is available at http://c.biancheng.net/python/.”’
  • print(longstr)

长字符串中的换行、空格、缩进等空白符都会原样输出,所以你不能写成下面的样子:

  • longstr = ”’
  • It took me 6 months to write this Python tutorial.
  • Please give me a to ‘thumb’ to keep it updated.
  • The Python tutorial is available at http://c.biancheng.net/python/.
  • ”’
  • print(longstr)

虽然这样写格式优美,但是输出结果将变成:

    It took me 6 months to write this Python tutorial.    Please give me a to ‘thumb’ to keep it updated.    The Python tutorial is available at http://c.biancheng.net/python/. 

字符串内容前后多出了两个空行,并且每一行的前面会多出四个空格。

五、Python原始字符串

Python 字符串中的反斜杠有着特殊的作用,就是转义字符,例如上面提到的和转义字符有时候会带来一些麻烦,例如我要表示一个包含 Windows 路径这样的字符串,在 Python 程序中直接这样写肯定是不行的,不管是普通字符串还是长字符串。因为的特殊性,我们需要对字符串中的每个都进行转义,也就是写成这种形式才行。这种写法需要特别谨慎,稍有疏忽就会出错。为了解决转义字符的问题,Python 支持原始字符串。在原始字符串中,不会被当作转义字符,所有的内容都保持“原汁原味”的样子。在普通字符串或者长字符串的开头加上前缀,就变成了原始字符串,具体格式为:
str1 = r’原始字符串内容’str2 = r”””原始字符串内容”””
将上面的 Windows 路径改写成原始字符串的形式:

rstr = r’D:\Program Files\Python 3.8\python.exe’
print(rstr)

原始字符串中的引号

如果普通格式的原始字符串中出现引号,程序同样需要对引号进行转义,否则 Python 照样无法对字符串的引号精确配对;但是和普通字符串不同的是,此时用于转义的反斜杠会变成字符串内容的一部分。请看下面的代码:

str1 = r’I\’m a great coder!’
print(str1)

输出结果:
I\’m a great coder!

需要注意的是,Python 原始字符串中的反斜杠仍然会对引号进行转义,因此原始字符串的结尾处不能是反斜杠,否则字符串结尾处的引号会被转义,导致字符串不能正确结束。在 Python 中有两种方式解决这个问题:一种方式是改用长字符串的写法,不要使用原始字符串;另一种方式是单独书写反斜杠,这是接下来要重点说明的。例如想表示,可以这样写:

str1 = r’D:\Program Files\Python 3.8′ ‘\\’
print(str1)

我们先写了一个原始字符串,紧接着又使用写了一个包含转义字符的普通字符串,Python 会自动将这两个字符串拼接在一起,所以上面代码的输出结果是:
D:\Program Files\Python 3.8\

Python 字符串中的反斜杠有着特殊的作用,就是转义字符,例如上面提到的和转义字符有时候会带来一些麻烦,例如我要表示一个包含 Windows 路径这样的字符串,在 Python 程序中直接这样写肯定是不行的,不管是普通字符串还是长字符串。因为的特殊性,我们需要对字符串中的每个都进行转义,也就是写成这种形式才行。这种写法需要特别谨慎,稍有疏忽就会出错。为了解决转义字符的问题,Python 支持原始字符串。在原始字符串中,不会被当作转义字符,所有的内容都保持“原汁原味”的样子。在普通字符串或者长字符串的开头加上前缀,就变成了原始字符串,具体格式为:

\
\'
\"
D:\Program Files\Python 3.8\python.exe
\
\
D:\\Program Files\\Python 3.8\\python.exe
\
r

str1 = r’原始字符串内容’str2 = r”””原始字符串内容”””

将上面的 Windows 路径改写成原始字符串的形式:

  • rstr = r’D:\Program Files\Python 3.8\python.exe’
  • print(rstr)

原始字符串中的引号

如果普通格式的原始字符串中出现引号,程序同样需要对引号进行转义,否则 Python 照样无法对字符串的引号精确配对;但是和普通字符串不同的是,此时用于转义的反斜杠会变成字符串内容的一部分。请看下面的代码:

  • str1 = r’I\’m a great coder!’
  • print(str1)

输出结果:

I\’m a great coder!

需要注意的是,Python 原始字符串中的反斜杠仍然会对引号进行转义,因此原始字符串的结尾处不能是反斜杠,否则字符串结尾处的引号会被转义,导致字符串不能正确结束。在 Python 中有两种方式解决这个问题:一种方式是改用长字符串的写法,不要使用原始字符串;另一种方式是单独书写反斜杠,这是接下来要重点说明的。例如想表示,可以这样写:

D:\Program Files\Python 3.8\
  • str1 = r’D:\Program Files\Python 3.8′ ‘\\’
  • print(str1)

我们先写了一个原始字符串,紧接着又使用写了一个包含转义字符的普通字符串,Python 会自动将这两个字符串拼接在一起,所以上面代码的输出结果是:

r'D:\Program Files\Python 3.8'
'\\'

D:\Program Files\Python 3.8\

————————

1、 What is a string

A set of several characters is a string. Python   The string in must be enclosed by double quotation marks or single quotation marks. The specific format is: “string content” and “string content”
The content of the string can contain letters, punctuation, special symbols, Chinese, Japanese and other words all over the world
Legal string: “123789”, “123abc”“ http://c.biancheng.net/python/ “C language Chinese network” has been established for 8 years“
There is no difference between double quotation marks and single quotation marks in Python strings

A set of several characters is a string. Python   The string in must be enclosed by double quotation marks or single quotation marks. The specific format is: “string content” and “string content”

" "
' '

The content of the string can contain letters, punctuation, special symbols, Chinese, Japanese and other words all over the world

Legal string: “123789”, “123abc”“ http://c.biancheng.net/python/ “C language Chinese network” has been established for 8 years“

There is no difference between double quotation marks and single quotation marks in Python strings

2、 Handle quotation marks in strings

1) Escape quotation marks
Adding a backslash before the quotation mark can escape the quotation mark and let Python treat it as normal text, for example:
str1 = ‘I\’m a great coder!’
STR2 = “quotation double quotation marks are \”, Chinese double quotation marks are““
print(str1)
print(str2)
Operation results:
I’m a great coder“
2) Enclose the string with different quotation marks
If single quotation marks appear in the string content, we can use double quotation marks to surround the string and vice versa. For example:
STR1 = “I’m a great coder!” # use double quotation marks to enclose strings with single quotation marks
STR2 = ‘quotation double quotation marks are “, Chinese double quotation marks are”‘ # use single quotation marks to enclose strings containing double quotation marks
print(str1)
print(str2)
The running result is the same as above.

1) Escape quotation marks

Adding a backslash before the quotation mark can escape the quotation mark and let Python treat it as normal text, for example:

\
  • str1 = ‘I\’m a great coder!’
  • STR2 = “quotation double quotation marks are \”, Chinese double quotation marks are““
  • print(str1)
  • print(str2)

Operation results:

I’m a great coder! Quotation double quotation marks are “, Chinese double quotation marks are”“

2) Enclose the string with different quotation marks

If a single quotation mark appears in the string content, we can use double quotation marks to surround the string, and vice versa. For example:

  • STR1 = “I’m a great coder!” # use double quotation marks to enclose strings with single quotation marks
  • STR2 = ‘quotation double quotation marks are “, Chinese double quotation marks are”‘ # use single quotation marks to enclose strings containing double quotation marks
  • print(str1)
  • print(str2)

The running result is the same as above.

3、 Newline of string

Python is not a free-form language. It has strict syntax requirements for line feed and indentation of programs. To wrap a long string, you must add a backslash at the end of the line. See the following example:
s2 = ‘It took me six months to write this Python tutorial. \
Please give me more support. \
I will keep it updated.’
The S2 string above is relatively long, so the escape character is used to wrap the string content, so that a long string can be written into multiple lines.
In addition, python also supports line wrapping of expressions, such as:
num = 20 + 3 / 4 + \
2 * 3
print(num)

Python is not a free-form language. It has strict syntax requirements for line feed and indentation of programs. To wrap a long string, you must add a backslash at the end of the line. See the following example:

\
  • s2 = ‘It took me six months to write this Python tutorial. \
  • Please give me more support. \
  • I will keep it updated.’

The S2 string above is relatively long, so the escape character is used to wrap the string content, so that a long string can be written into multiple lines.

\

In addition, python also supports line wrapping of expressions, such as:

  • num = 20 + 3 / 4 + \
  • 2 * 3
  • print(num)

4、 Python long string

Using three single quotation marks or double quotation marks can annotate multi line content, which is actually the writing method of Python long string. The so-called long string is a string that can be written directly on a new line (without adding a backslash). Python long string is surrounded by three double quotes or three single quotes. The syntax format is as follows:
“” “long string content” “” “” long string content ””
Placing single or double quotation marks in long strings does not cause parsing errors. If the long string is not assigned to any variable, the long string will not play any role. It is no different from an ordinary text and is equivalent to being commented out.
Note that at this time, the Python interpreter will not ignore the long string and will parse it according to the syntax, but the long string does not play a practical role.
When a large piece of text in the program needs to be defined as a string, it is preferred to use the long string form, because this form is very powerful and can place any content in the string, including single quotation marks and double quotation marks. [example] assign a long string to a variable:
longstr = ”’It took me 6 months to write this Python tutorial.
Please give me a to ‘thumb’ to keep it updated.
The Python tutorial is available at http://c.biancheng.net/python/. ”’
print(longstr)
The white space characters such as newline, space and indentation in the long string will be output as is, so you can’t write it as follows:
longstr = ”’
It took me 6 months to write this Python tutorial.
Please give me a to ‘thumb’ to keep it updated.
The Python tutorial is available at http://c.biancheng.net/python/.
”’
print(longstr)
Although the format is beautiful, the output result will become:
    It took me 6 months to write this Python tutorial.     Please give me a to ‘thumb’ to keep it updated.     The Python tutorial is available at http://c.biancheng.net/python/.  
There are two more empty lines before and after the string content, and four more spaces will be in front of each line.

Using three single quotation marks or double quotation marks can annotate multi line content, which is actually the writing method of Python long string. The so-called long string is a string that can be written directly on a new line (without adding a backslash). Python long string is surrounded by three double quotes or three single quotes. The syntax format is as follows:

\
"""
'''

“” “long string content” “” “” long string content ””

Placing single or double quotation marks in long strings does not cause parsing errors. If the long string is not assigned to any variable, the long string will not play any role. It is no different from an ordinary text and is equivalent to being commented out.

Note that at this time, the Python interpreter will not ignore the long string and will parse it according to the syntax, but the long string does not play a practical role.

When a large piece of text in the program needs to be defined as a string, it is preferred to use the long string form, because this form is very powerful and can place any content in the string, including single quotation marks and double quotation marks. [example] assign a long string to a variable:

  • longstr = ”’It took me 6 months to write this Python tutorial.
  • Please give me a to ‘thumb’ to keep it updated.
  • The Python tutorial is available at http://c.biancheng.net/python/.”’
  • print(longstr)

The white space characters such as newline, space and indentation in the long string will be output as is, so you can’t write it as follows:

  • longstr = ”’
  • It took me 6 months to write this Python tutorial.
  • Please give me a to ‘thumb’ to keep it updated.
  • The Python tutorial is available at http://c.biancheng.net/python/.
  • ”’
  • print(longstr)

Although the format is beautiful, the output result will become:

    It took me 6 months to write this Python tutorial.    Please give me a to ‘thumb’ to keep it updated.    The Python tutorial is available at http://c.biancheng.net/python/. 

There are two more empty lines before and after the string content, and four more spaces will be in front of each line.

5、 Python raw string

Backslashes in Python strings have a special function, that is, escape characters. For example, the above-mentioned and escape characters sometimes bring some trouble. For example, if I want to represent a string containing a Windows path, it is certainly not OK to write it directly in Python programs, whether it is an ordinary string or a long string. Because of its particularity, we need to escape each string, that is, write it in this form. This kind of writing needs special caution. A little negligence will make mistakes. To solve the problem of escaping characters, python supports raw strings. In the original string, it will not be treated as an escape character, and all contents will remain “original”. Adding a prefix to the beginning of an ordinary string or a long string becomes the original string. The specific format is:
STR1 = R ‘original string content’ STR2 = R “” original string content “” ”
Rewrite the above windows path to the original string:
rstr = r’D:\Program Files\Python 3.8\python.exe’
print(rstr)
Quotation marks in the original string
If quotation marks appear in the original string in normal format, the program also needs to escape the quotation marks, otherwise Python still cannot accurately pair the quotation marks of the string; However, unlike ordinary strings, the backslash used for escape will become part of the string content. See the following code:
str1 = r’I\’m a great coder!’
print(str1)
Output results:
I\’m a great coder!
It should be noted that the backslash in the python original string will still escape the quotation marks, so the end of the original string cannot be a backslash, otherwise the quotation marks at the end of the string will be escaped, resulting in the incorrect end of the string. There are two ways to solve this problem in Python: one way is to use the long string instead of the original string; Another way is to write backslashes separately, which will be highlighted next. For example, if you want to express, you can write as follows:
str1 = r’D:\Program Files\Python 3.8′ ‘\\’
print(str1)
We first write an original string, and then write an ordinary string containing escape characters. Python will automatically splice the two strings together, so the output result of the above code is:
D:\Program Files\Python 3.8\

Backslashes in Python strings have a special function, that is, escape characters. For example, the above-mentioned and escape characters sometimes bring some trouble. For example, if I want to represent a string containing a Windows path, it is certainly not OK to write it directly in Python programs, whether it is an ordinary string or a long string. Because of its particularity, we need to escape each string, that is, write it in this form. This kind of writing needs special caution. A little negligence will make mistakes. To solve the problem of escaping characters, python supports raw strings. In the original string, it will not be treated as an escape character, and all contents will remain “original”. Adding a prefix to the beginning of an ordinary string or a long string becomes the original string. The specific format is:

\
\'
\"
D:\Program Files\Python 3.8\python.exe
\
\
D:\\Program Files\\Python 3.8\\python.exe
\
r

STR1 = R ‘original string content’ STR2 = R “” original string content “” “

Rewrite the above windows path to the original string:

  • rstr = r’D:\Program Files\Python 3.8\python.exe’
  • print(rstr)

Quotation marks in the original string

If quotation marks appear in the original string in normal format, the program also needs to escape the quotation marks, otherwise Python still cannot accurately pair the quotation marks of the string; However, unlike ordinary strings, the backslash used for escape will become part of the string content. See the following code:

  • str1 = r’I\’m a great coder!’
  • print(str1)

Output results:

I\’m a great coder!

It should be noted that the backslash in the python original string will still escape the quotation marks, so the end of the original string cannot be a backslash, otherwise the quotation marks at the end of the string will be escaped, resulting in the incorrect end of the string. There are two ways to solve this problem in Python: one way is to use the long string instead of the original string; Another way is to write backslashes separately, which will be highlighted next. For example, if you want to express, you can write as follows:

D:\Program Files\Python 3.8\
  • str1 = r’D:\Program Files\Python 3.8′ ‘\\’
  • print(str1)

We first write an original string, and then write an ordinary string containing escape characters. Python will automatically splice the two strings together, so the output result of the above code is:

r'D:\Program Files\Python 3.8'
'\\'

D:\Program Files\Python 3.8\