Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python3 string


May 10, 2021 Python3


Table of contents


Python string

In addition to numbers, Python can also manipulate strings. Strings can be expressed in several ways, between single and double quotes:

>>> 'spam eggs'
'spam eggs'
>>> 'doesn\'t'
"doesn't"
>>> "doesn't"
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said."
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'

Python uses backslash escape quotes and other special characters to accurately represent them.

If a string contains single quotes but no double quotes, the string is enclosed in double quotes, otherwise it is enclosed in single quotes. F or such input strings, the print() function produces more readable output.

Literal strings across lines can be represented in several ways. U sing a continuation character, that is, a backslash is used after the last character of each line to indicate that the next line is a logical continuation of the last line:

Here's how to add a new line with .

>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
>>> print('"Isn\'t," she said.')
"Isn't," she said.
>>> s = 'First line.\nSecond line.'  # \n 意味着新行
>>> s  # 不使用 print(), \n 包含在输出中
'First line.\nSecond line.'
>>> print(s)  # 使用 print(), \n 输出一个新行
First line.
Second line.

Here's a backslash to continue the line:

hello = "This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
    Note that whitespace at the beginning of the line is\
 significant."

print(hello)

Note that the line breaks in it are still to be represented by a line break after the backslash that has been discarded. T he above examples will be output as follows:

This is a rather long string containing
several lines of text just as you would do in C.
    Note that whitespace at the beginning of the line is significant.

Alternatively, strings can be enclosed by "" (three double quotes) or '' (three single quotes). W hen you use three quotation marks, line breaks do not need to be escaped, they are included in the string. T he following example uses an escape character to avoid generating an unwanted empty line at the beginning.

print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")

The output is as follows:

Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

If we use the "original" string, then the backslash at the end of the line, and the line break in the source code, will not be converted to line breaks, which will be included as data in the string. F or example:

hello = r"This is a rather long string containing\n\
several lines of text much as you would do in C."

print(hello)

Will output:

This is a rather long string containing\n\
several lines of text much as you would do in C.

Strings can be connected together using a string of operators, or repeated with the operator:

>>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> '<' + word*5 + '>'
''

Two adjacent literal strings are automatically serialted; the first line of the example above can also be written as word 'Help' 'A';

>>> 'str' 'ing'                   
#  <- string="">>> 'str'.strip() + 'ing'   
#  <- string="">>> 'str'.strip() 'ing'     
#  <-  这样操作错误   File "", line 1, in ?
    'str'.strip() 'ing'
                      ^
SyntaxError: invalid syntax

Strings can be indexed; T here is no separate character type; L ike the Icon programming language, substrings can be specified by dividers: two indexes separated by colons.

>>> word[4]
'A'
>>> word[0:2]
'He'
>>> word[2:4]
'lp'

The default single index is useful: the default first index is zero, and the second index defaults to the length at which strings can be sliced.

>>> word[:2]    # 前两个字符
'He'
>>> word[2:]    # 除了前两个字符之外,其后的所有字符
'lpA'

Unlike the C string, the Python string cannot be changed. A ssigning a value to an index location results in an error:

>>> word[0] = 'x'
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: 'str' object does not support item assignment
>>> word[:1] = 'Splat'
Traceback (most recent call last):
  File "", line 1, in ?
TypeError: 'str' object does not support slice assignment

However, creating a new string by combining content is simple and efficient:

>>> 'x' + word[1:]
'xelpA'
>>> 'Splat' + word[4]
'SplatA'
在分切操作字符串时,有一个很有用的规律: s[:i] + s[i:] 等于 s.

>>> word[:2] + word[2:]
'HelpA'
>>> word[:3] + word[3:]
'HelpA'

The handling of biased sachet indexes is also elegant: an oversized index will be replaced by the size of the string, and an empty string will be returned if the upper limit is less than the lower limit.

>>> word[1:100]
'elpA'
>>> word[10:]

>>> word[2:1]

Negative numbers can be used in the index, which counts from right to left. F or example:

>>> word[-1]     # 最后一个字符
'A'
>>> word[-2]     # 倒数第二个字符
'p'
>>> word[-2:]    # 最后两个字符
'pA'
>>> word[:-2]    # 除了最后两个字符之外,其前面的所有字符
'Hel'
但要注意, -00 完全一样,所以 -0 不会从右开始计数!

>>> word[-0]     # (既然 -0 等于 0)
'H'

Out-of-range negative indexes are truncated, but don't try to use them in a single-element index (non-sliced index):

>>> word[-100:]
'HelpA'
>>> word[-10]    # 错误
Traceback (most recent call last):
  File "", line 1, in ?
IndexError: string index out of range

There is a way for you to remember how a se through index works, imagine that the index is pointing between characters, and the number to the left of the first character is 0. T hen, the last character of a string with n characters is index n to the right, for example:

 +---+---+---+---+---+
 | H | e | l | p | A |
 +---+---+---+---+---+
 0   1   2   3   4   5
-5  -4  -3  -2  -1

The number 0...5 on the first line gives the position of the index in the string; T he parting section consists of all the characters marked i and j at the edges from i to j, respectively.

For non-negative parts, if the index is within the valid range, the length of the part is the difference between the indexes. F or example, the length of word 1:3 is 2.

The built-in function len() returns the length of a string:

>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34