Bộ đếm bảng chữ cái python

Mô-đun chứa một số hằng số và lớp hữu ích, cũng như một số hàm kế thừa không dùng nữa cũng có sẵn dưới dạng phương thức trên chuỗi. Ngoài ra, các lớp chuỗi dựng sẵn của Python hỗ trợ các phương thức kiểu chuỗi được mô tả trong phần này và cả các phương thức dành riêng cho chuỗi được mô tả trong phần. Để xuất các chuỗi có định dạng, hãy sử dụng các chuỗi mẫu hoặc toán tử

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
9 được mô tả trong phần. Ngoài ra, hãy xem mô-đun để biết các hàm chuỗi dựa trên biểu thức chính quy

7. 1. 1. Hằng chuỗi

Các hằng số được xác định trong mô-đun này là

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
2

Phép nối của hằng và hằng được mô tả dưới đây. Giá trị này không phụ thuộc vào ngôn ngữ

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
6

Các chữ thường

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
7. Giá trị này không phụ thuộc vào ngôn ngữ và sẽ không thay đổi

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
9

Các chữ hoa

format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
0. Giá trị này không phụ thuộc vào ngôn ngữ và sẽ không thay đổi

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
2

Chuỗi

format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
3

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
5

Chuỗi

format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
6

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
8

Sự nối của các chuỗi và được mô tả dưới đây. Giá trị cụ thể phụ thuộc vào ngôn ngữ và sẽ được cập nhật khi được gọi

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
>>> '{0}, {1}, {2}'.format['a', 'b', 'c']
'a, b, c'
>>> '{}, {}, {}'.format['a', 'b', 'c']  # 2.7+ only
'a, b, c'
>>> '{2}, {1}, {0}'.format['a', 'b', 'c']
'c, b, a'
>>> '{2}, {1}, {0}'.format[*'abc']      # unpacking argument sequence
'c, b, a'
>>> '{0}{1}{0}'.format['abra', 'cad']   # arguments' indices can be repeated
'abracadabra'
3

Một chuỗi chứa tất cả các ký tự được coi là chữ thường. Trên hầu hết các hệ thống, đây là chuỗi

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
7. Giá trị cụ thể phụ thuộc vào ngôn ngữ và sẽ được cập nhật khi được gọi

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
>>> '{0}, {1}, {2}'.format['a', 'b', 'c']
'a, b, c'
>>> '{}, {}, {}'.format['a', 'b', 'c']  # 2.7+ only
'a, b, c'
>>> '{2}, {1}, {0}'.format['a', 'b', 'c']
'c, b, a'
>>> '{2}, {1}, {0}'.format[*'abc']      # unpacking argument sequence
'c, b, a'
>>> '{0}{1}{0}'.format['abra', 'cad']   # arguments' indices can be repeated
'abracadabra'
7

Chuỗi

>>> '{0}, {1}, {2}'.format['a', 'b', 'c']
'a, b, c'
>>> '{}, {}, {}'.format['a', 'b', 'c']  # 2.7+ only
'a, b, c'
>>> '{2}, {1}, {0}'.format['a', 'b', 'c']
'c, b, a'
>>> '{2}, {1}, {0}'.format[*'abc']      # unpacking argument sequence
'c, b, a'
>>> '{0}{1}{0}'.format['abra', 'cad']   # arguments' indices can be repeated
'abracadabra'
8

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
>>> 'Coordinates: {latitude}, {longitude}'.format[latitude='37.24N', longitude='-115.81W']
'Coordinates: 37.24N, -115.81W'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format[**coord]
'Coordinates: 37.24N, -115.81W'
0

Chuỗi ký tự ASCII được coi là ký tự dấu chấm câu trong ngôn ngữ

>>> 'Coordinates: {latitude}, {longitude}'.format[latitude='37.24N', longitude='-115.81W']
'Coordinates: 37.24N, -115.81W'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format[**coord]
'Coordinates: 37.24N, -115.81W'
1

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
>>> 'Coordinates: {latitude}, {longitude}'.format[latitude='37.24N', longitude='-115.81W']
'Coordinates: 37.24N, -115.81W'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format[**coord]
'Coordinates: 37.24N, -115.81W'
3

Chuỗi ký tự được coi là có thể in được. Đây là sự kết hợp của , , , và

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
>>> 'Coordinates: {latitude}, {longitude}'.format[latitude='37.24N', longitude='-115.81W']
'Coordinates: 37.24N, -115.81W'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format[**coord]
'Coordinates: 37.24N, -115.81W'
9

Một chuỗi chứa tất cả các ký tự được coi là chữ in hoa. Trên hầu hết các hệ thống, đây là chuỗi

format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
0. Giá trị cụ thể phụ thuộc vào ngôn ngữ và sẽ được cập nhật khi được gọi

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
>>> c = 3-5j
>>> ['The complex number {0} is formed from the real part {0.real} '
..  'and the imaginary part {0.imag}.'].format[c]
'The complex number [3-5j] is formed from the real part 3.0 and the imaginary part -5.0.'
>>> class Point[object]:
..     def __init__[self, x, y]:
..         self.x, self.y = x, y
..     def __str__[self]:
..         return 'Point[{self.x}, {self.y}]'.format[self=self]
...
>>> str[Point[4, 2]]
'Point[4, 2]'
3

Một chuỗi chứa tất cả các ký tự được coi là khoảng trắng. Trên hầu hết các hệ thống, điều này bao gồm không gian ký tự, tab, nguồn cấp dữ liệu, trả về, nguồn cấp dữ liệu biểu mẫu và tab dọc

7. 1. 2. Định dạng chuỗi tùy chỉnh

Mới trong phiên bản 2. 6

Các lớp str và unicode tích hợp cung cấp khả năng thực hiện thay thế biến phức tạp và định dạng giá trị thông qua phương thức được mô tả trong PEP 3101. Lớp trong mô-đun cho phép bạn tạo và tùy chỉnh các hành vi định dạng chuỗi của riêng mình bằng cách sử dụng cách triển khai giống như phương thức tích hợp sẵn

lớp
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1_______32_______9

Lớp có các phương thức công khai sau

>>> coord = [3, 5]
>>> 'X: {0[0]};  Y: {0[1]}'.format[coord]
'X: 3;  Y: 5'
1 [ format_string, *args, **kwargs ]

Phương pháp API chính. Nó nhận một chuỗi định dạng và một tập hợp tùy ý các đối số vị trí và từ khóa. Nó chỉ là một trình bao bọc gọi

>>> coord = [3, 5]
>>> 'X: {0[0]};  Y: {0[1]}'.format[coord]
'X: 3;  Y: 5'
3 [ format_string, args, kwargs ]

Chức năng này thực hiện công việc định dạng thực tế. Nó được hiển thị dưới dạng một hàm riêng biệt cho các trường hợp bạn muốn chuyển vào một từ điển các đối số được xác định trước, thay vì giải nén và đóng gói lại từ điển dưới dạng các đối số riêng lẻ bằng cách sử dụng cú pháp

>>> coord = [3, 5]
>>> 'X: {0[0]};  Y: {0[1]}'.format[coord]
'X: 3;  Y: 5'
4 và
>>> coord = [3, 5]
>>> 'X: {0[0]};  Y: {0[1]}'.format[coord]
'X: 3;  Y: 5'
5. thực hiện công việc chia nhỏ chuỗi định dạng thành dữ liệu ký tự và các trường thay thế. Nó gọi các phương thức khác nhau được mô tả bên dưới

Ngoài ra, định nghĩa một số phương thức được dự định thay thế bởi các lớp con

>>> coord = [3, 5]
>>> 'X: {0[0]};  Y: {0[1]}'.format[coord]
'X: 3;  Y: 5'
8 [ format_string ]

Lặp lại format_string và trả về một bộ lặp có thể lặp lại [literal_text, field_name, format_spec, chuyển đổi]. Điều này được sử dụng để ngắt chuỗi thành văn bản bằng chữ hoặc trường thay thế

Các giá trị trong bộ dữ liệu biểu thị một cách khái niệm một khoảng văn bản theo sau là một trường thay thế duy nhất. Nếu không có văn bản bằng chữ [điều này có thể xảy ra nếu hai trường thay thế xảy ra liên tiếp], thì văn bản bằng chữ sẽ là một chuỗi có độ dài bằng không. Nếu không có trường thay thế thì giá trị của field_name, format_spec và chuyển đổi sẽ là

>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
0

>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
1 [ tên_trường, args, kwargs ]

Đã cho field_name như được trả về bởi [xem ở trên], chuyển đổi nó thành một đối tượng được định dạng. Trả về một bộ [obj, used_key]. Phiên bản mặc định lấy các chuỗi có dạng được xác định trong PEP 3101, chẳng hạn như “0[tên]” hoặc “nhãn. chức vụ". args và kwargs như được truyền vào. Giá trị trả về used_key có cùng ý nghĩa với tham số chính để

>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
5 [ key, args, kwargs ]

Truy xuất một giá trị trường đã cho. Đối số chính sẽ là một số nguyên hoặc một chuỗi. Nếu nó là một số nguyên, nó đại diện cho chỉ mục của đối số vị trí trong args;

Tham số args được đặt thành danh sách các đối số vị trí thành và tham số kwargs được đặt thành từ điển của các đối số từ khóa

Đối với tên trường ghép, các hàm này chỉ được gọi cho thành phần đầu tiên của tên trường;

Vì vậy, ví dụ, biểu thức trường '0. name' sẽ được gọi với đối số chính là 0. Thuộc tính

>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
8 sẽ được tra cứu sau khi trả về bằng cách gọi hàm tích hợp

Nếu chỉ mục hoặc từ khóa đề cập đến một mục không tồn tại, thì nên tăng hoặc

>>> '{:30}'.format['right aligned']
'                 right aligned'
>>> '{:^30}'.format['centered']
'           centered           '
>>> '{:*^30}'.format['centered']  # use '*' as a fill char
'***********centered***********'
3 [ used_args, args, kwargs ]

Thực hiện kiểm tra các đối số không sử dụng nếu muốn. Đối số của hàm này là tập hợp tất cả các khóa đối số thực sự được tham chiếu trong chuỗi định dạng [số nguyên cho đối số vị trí và chuỗi cho đối số được đặt tên] và tham chiếu đến đối số và kwargs đã được chuyển đến vformat. Tập hợp các đối số không sử dụng có thể được tính từ các tham số này. được cho là đưa ra một ngoại lệ nếu kiểm tra không thành công

>>> '{:30}'.format['right aligned']
'                 right aligned'
>>> '{:^30}'.format['centered']
'           centered           '
>>> '{:*^30}'.format['centered']  # use '*' as a fill char
'***********centered***********'
5 [ giá trị, định dạng_spec ]

chỉ cần gọi tích hợp toàn cầu. Phương thức được cung cấp để các lớp con có thể ghi đè lên nó

>>> '{:30}'.format['right aligned']
'                 right aligned'
>>> '{:^30}'.format['centered']
'           centered           '
>>> '{:*^30}'.format['centered']  # use '*' as a fill char
'***********centered***********'
8 [ giá trị, chuyển đổi ]

Chuyển đổi giá trị [được trả về bởi ] cho một loại chuyển đổi [như trong bộ dữ liệu được trả về bởi phương thức]. Phiên bản mặc định hiểu các loại chuyển đổi 's' [str], 'r' [repr] và 'a' [ascii]

7. 1. 3. Cú pháp chuỗi định dạng

Phương thức và lớp chia sẻ cùng một cú pháp cho các chuỗi định dạng [mặc dù trong trường hợp của , các lớp con có thể xác định cú pháp chuỗi định dạng của riêng chúng]

Chuỗi định dạng chứa “trường thay thế” được bao quanh bởi dấu ngoặc nhọn

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
04. Bất cứ thứ gì không có trong dấu ngoặc nhọn được coi là văn bản theo nghĩa đen, được sao chép không thay đổi vào đầu ra. Nếu bạn cần bao gồm một ký tự ngoặc nhọn trong văn bản chữ, nó có thể được thoát ra bằng cách nhân đôi.
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
05 và
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
06

Ngữ pháp cho trường thay thế như sau

replacement_field ::=  "{" [field_name] ["!" conversion] [":" format_spec] "}"
field_name        ::=  arg_name ["." attribute_name | "[" element_index "]"]*
arg_name          ::=  [identifier | integer]
attribute_name    ::=  identifier
element_index     ::=  integer | index_string
index_string      ::=   +
conversion        ::=  "r" | "s"
format_spec       ::=  

Nói một cách ít trang trọng hơn, trường thay thế có thể bắt đầu bằng tên_trường chỉ định đối tượng có giá trị sẽ được định dạng và chèn vào đầu ra thay vì trường thay thế. Trường_tên tùy chọn được theo sau bởi trường chuyển đổi, đứng trước dấu chấm than

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
07 và định dạng_spec, đứng trước dấu hai chấm
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
08. Chúng chỉ định định dạng không mặc định cho giá trị thay thế

Xem thêm phần

Bản thân field_name bắt đầu bằng arg_name là một số hoặc một từ khóa. Nếu đó là một số, nó đề cập đến một đối số vị trí và nếu đó là một từ khóa, thì nó đề cập đến một đối số từ khóa được đặt tên. Nếu arg_names số trong một chuỗi định dạng là 0, 1, 2, … theo thứ tự, tất cả chúng có thể được bỏ qua [không chỉ một số] và các số 0, 1, 2, … sẽ tự động được chèn vào theo thứ tự đó. Vì arg_name không được phân tách bằng dấu ngoặc kép nên không thể chỉ định các khóa từ điển tùy ý [e. g. , các chuỗi

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
09 hoặc
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
10] trong một chuỗi định dạng. Arg_name có thể được theo sau bởi bất kỳ số lượng biểu thức thuộc tính hoặc chỉ mục nào. Một biểu thức có dạng
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
11 chọn thuộc tính được đặt tên bằng cách sử dụng , trong khi một biểu thức có dạng
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
13 thực hiện tra cứu chỉ mục bằng cách sử dụng

Đã thay đổi trong phiên bản 2. 7. Có thể bỏ qua các từ chỉ định đối số vị trí cho và

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
16, vì vậy
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
17 tương đương với
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
18,
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
19 tương đương với ________0____20.

Một số ví dụ chuỗi định dạng đơn giản

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.

Trường chuyển đổi gây ra sự ép buộc kiểu trước khi định dạng. Thông thường, công việc định dạng một giá trị được thực hiện bằng phương thức

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
21 của chính giá trị đó. Tuy nhiên, trong một số trường hợp, nên buộc một loại được định dạng dưới dạng một chuỗi, ghi đè định nghĩa định dạng của chính nó. Bằng cách chuyển đổi giá trị thành một chuỗi trước khi gọi
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
21, logic định dạng thông thường được bỏ qua

Hai cờ chuyển đổi hiện được hỗ trợ.

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
23 gọi giá trị và
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
25 gọi

Vài ví dụ

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first

Trường format_spec chứa thông số kỹ thuật về cách trình bày giá trị, bao gồm các chi tiết như độ rộng trường, căn chỉnh, phần đệm, độ chính xác thập phân, v.v. Mỗi loại giá trị có thể xác định “ngôn ngữ nhỏ định dạng” của riêng mình hoặc diễn giải định dạng_spec

Hầu hết các loại tích hợp đều hỗ trợ một ngôn ngữ nhỏ định dạng phổ biến, được mô tả trong phần tiếp theo

Trường format_spec cũng có thể bao gồm các trường thay thế lồng nhau bên trong nó. Các trường thay thế lồng nhau này có thể chứa tên trường, cờ chuyển đổi và đặc tả định dạng nhưng không được phép lồng sâu hơn. Các trường thay thế trong format_spec được thay thế trước khi chuỗi format_spec được diễn giải. Điều này cho phép định dạng của một giá trị được chỉ định động

Xem phần cho một số ví dụ

7. 1. 3. 1. Ngôn ngữ nhỏ đặc tả định dạng

"Thông số kỹ thuật định dạng" được sử dụng trong các trường thay thế có trong chuỗi định dạng để xác định cách trình bày các giá trị riêng lẻ [xem phần ]. Chúng cũng có thể được chuyển trực tiếp đến hàm tích hợp. Mỗi loại có thể định dạng có thể xác định cách diễn giải đặc tả định dạng

Hầu hết các loại tích hợp đều triển khai các tùy chọn sau cho thông số kỹ thuật định dạng, mặc dù một số tùy chọn định dạng chỉ được hỗ trợ bởi các loại số

Một quy ước chung là một chuỗi định dạng trống [

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
28] tạo ra kết quả tương tự như thể bạn đã gọi giá trị. Chuỗi định dạng không trống thường sửa đổi kết quả

Hình thức chung của một công cụ xác định định dạng tiêu chuẩn là

________số 8_______

Nếu một giá trị căn chỉnh hợp lệ được chỉ định, nó có thể được bắt đầu bằng một ký tự điền có thể là bất kỳ ký tự nào và mặc định là khoảng trắng nếu bị bỏ qua. Không thể sử dụng dấu ngoặc nhọn [“

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
30” hoặc “
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
31”] làm ký tự điền khi sử dụng phương thức. Tuy nhiên, có thể chèn dấu ngoặc nhọn với trường thay thế lồng nhau. Hạn chế này không ảnh hưởng đến chức năng

Ý nghĩa của các tùy chọn căn chỉnh khác nhau như sau

Quyền mua

Nghĩa

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
34

Buộc trường phải được căn trái trong không gian có sẵn [đây là mặc định cho hầu hết các đối tượng]

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
35

Buộc trường phải được căn phải trong khoảng trống có sẵn [đây là giá trị mặc định cho các số]

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
36

Buộc đặt phần đệm sau dấu [nếu có] nhưng trước các chữ số. Điều này được sử dụng để in các trường ở dạng '+000000120'. Tùy chọn căn chỉnh này chỉ hợp lệ cho các loại số. Nó trở thành mặc định khi '0' ngay trước độ rộng trường

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
37

Buộc trường được căn giữa trong không gian có sẵn

Lưu ý rằng trừ khi độ rộng trường tối thiểu được xác định, chiều rộng trường sẽ luôn có cùng kích thước với dữ liệu để điền vào, do đó, tùy chọn căn chỉnh không có ý nghĩa gì trong trường hợp này

Tùy chọn ký hiệu chỉ hợp lệ đối với các loại số và có thể là một trong các tùy chọn sau

Quyền mua

Nghĩa

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
38

chỉ ra rằng một dấu hiệu nên được sử dụng cho cả số dương cũng như số âm

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
39

chỉ ra rằng một dấu hiệu chỉ nên được sử dụng cho các số âm [đây là hành vi mặc định]

không gian

chỉ ra rằng nên sử dụng khoảng trắng ở đầu trên các số dương và dấu trừ trên các số âm

Tùy chọn

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
40 chỉ hợp lệ cho số nguyên và chỉ cho đầu ra nhị phân, bát phân hoặc thập lục phân. Nếu có, nó chỉ định rằng đầu ra sẽ có tiền tố lần lượt là
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
41,
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
42 hoặc
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
43

Tùy chọn

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
44 báo hiệu việc sử dụng dấu phẩy cho dấu phân cách hàng nghìn. Đối với dấu phân cách nhận biết ngôn ngữ, thay vào đó hãy sử dụng kiểu trình bày số nguyên
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
45

Đã thay đổi trong phiên bản 2. 7. Đã thêm tùy chọn

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
44 [xem thêm PEP 378].

chiều rộng là một số nguyên thập phân xác định chiều rộng trường tối thiểu. Nếu không được chỉ định, thì độ rộng trường sẽ được xác định bởi nội dung

Khi không căn chỉnh rõ ràng, trước trường chiều rộng bằng ký tự số 0 [

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
47] cho phép đệm số 0 nhận biết dấu hiệu cho các loại số. Điều này tương đương với ký tự điền là
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
47 với kiểu căn chỉnh là
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
36

Độ chính xác là một số thập phân cho biết có bao nhiêu chữ số sẽ được hiển thị sau dấu thập phân đối với giá trị dấu phẩy động được định dạng bằng

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
50 và
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
51 hoặc trước và sau dấu thập phân đối với giá trị dấu phẩy động được định dạng bằng
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
52 hoặc
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
53. Đối với các loại không phải số, trường cho biết kích thước trường tối đa - nói cách khác, có bao nhiêu ký tự sẽ được sử dụng từ nội dung trường. Độ chính xác không được phép cho các giá trị số nguyên

Cuối cùng, loại xác định cách trình bày dữ liệu

Các kiểu trình bày chuỗi có sẵn là

Loại

Nghĩa

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
54

định dạng chuỗi. Đây là kiểu mặc định cho chuỗi và có thể bỏ qua

Không có

Giống như

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
54

Các kiểu trình bày số nguyên có sẵn là

Loại

Nghĩa

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
56

định dạng nhị phân. Xuất ra số trong cơ số 2

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
57

Tính cách. Chuyển đổi số nguyên thành ký tự unicode tương ứng trước khi in

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
58

số nguyên thập phân. Xuất ra số trong cơ số 10

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
59

định dạng bát phân. Xuất ra số trong cơ số 8

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
60

định dạng hex. Xuất ra số trong cơ số 16, sử dụng chữ thường cho các chữ số trên 9

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
61

định dạng hex. Xuất số trong cơ số 16, sử dụng chữ in hoa cho các chữ số trên 9

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
45

Con số. Điều này giống như

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
58, ngoại trừ việc nó sử dụng cài đặt ngôn ngữ hiện tại để chèn các ký tự phân cách số thích hợp

Không có

Giống như

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
58

Ngoài các kiểu trình bày ở trên, số nguyên có thể được định dạng bằng các kiểu trình bày dấu phẩy động được liệt kê bên dưới [ngoại trừ

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
45 và
>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
0]. Khi làm như vậy, được sử dụng để chuyển đổi số nguyên thành số dấu phẩy động trước khi định dạng

Các kiểu trình bày có sẵn cho dấu phẩy động và giá trị thập phân là

Loại

Nghĩa

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
68

ký hiệu số mũ. In số trong ký hiệu khoa học bằng chữ 'e' để biểu thị số mũ. Độ chính xác mặc định là

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
69

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
70

ký hiệu số mũ. Tương tự như

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
68 ngoại trừ nó sử dụng chữ hoa 'E' làm ký tự phân cách

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
50

Ký hiệu điểm cố định. Hiển thị số dưới dạng số điểm cố định. Độ chính xác mặc định là

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
69

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
51

Ký hiệu điểm cố định. Tương tự như

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
50

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
52

định dạng chung. Đối với một độ chính xác nhất định

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
77, điều này làm tròn số thành
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
78 chữ số có nghĩa và sau đó định dạng kết quả ở định dạng điểm cố định hoặc ký hiệu khoa học, tùy thuộc vào độ lớn của nó

Các quy tắc chính xác như sau. giả sử rằng kết quả được định dạng với kiểu trình bày

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
68 và độ chính xác
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
80 sẽ có số mũ
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
81. Sau đó, nếu
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
82, số được định dạng với kiểu trình bày
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
50 và độ chính xác
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
84. Mặt khác, số được định dạng với kiểu trình bày
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
68 và độ chính xác
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
80. Trong cả hai trường hợp, các số 0 ở cuối không đáng kể sẽ bị xóa khỏi ý nghĩa và dấu thập phân cũng bị xóa nếu không còn chữ số nào theo sau nó

Vô cực dương và âm, số 0 dương và âm, và nans, được định dạng lần lượt là

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
87,
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
88,
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
89,
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
90 và
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
91, bất kể độ chính xác là bao nhiêu

Độ chính xác của

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
89 được coi là tương đương với độ chính xác của
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
93. Độ chính xác mặc định là
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
69

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
53

định dạng chung. Tương tự như

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
52 ngoại trừ chuyển sang
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
70 nếu số quá lớn. Các biểu diễn của vô cực và NaN cũng được viết hoa

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
45

Con số. Điều này giống với

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
52, ngoại trừ việc nó sử dụng cài đặt ngôn ngữ hiện tại để chèn các ký tự phân tách số thích hợp

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
00

tỷ lệ phần trăm. Nhân một số với 100 và hiển thị ở định dạng cố định [_______0_______50], theo sau là dấu phần trăm

Không có

Giống như

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
52

7. 1. 3. 2. Ví dụ về định dạng

Phần này chứa các ví dụ về cú pháp và so sánh với định dạng

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
9 cũ

Trong hầu hết các trường hợp, cú pháp tương tự như định dạng

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
9 cũ, với việc bổ sung
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
04 và với
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
07 được sử dụng thay cho
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
9. Ví dụ:
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
09 có thể được dịch thành
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
10

Cú pháp định dạng mới cũng hỗ trợ các tùy chọn mới và khác nhau, được hiển thị trong các ví dụ sau

Truy cập đối số theo vị trí

>>> '{0}, {1}, {2}'.format['a', 'b', 'c']
'a, b, c'
>>> '{}, {}, {}'.format['a', 'b', 'c']  # 2.7+ only
'a, b, c'
>>> '{2}, {1}, {0}'.format['a', 'b', 'c']
'c, b, a'
>>> '{2}, {1}, {0}'.format[*'abc']      # unpacking argument sequence
'c, b, a'
>>> '{0}{1}{0}'.format['abra', 'cad']   # arguments' indices can be repeated
'abracadabra'

Truy cập đối số theo tên

>>> 'Coordinates: {latitude}, {longitude}'.format[latitude='37.24N', longitude='-115.81W']
'Coordinates: 37.24N, -115.81W'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format[**coord]
'Coordinates: 37.24N, -115.81W'

Truy cập các thuộc tính của đối số

>>> c = 3-5j
>>> ['The complex number {0} is formed from the real part {0.real} '
..  'and the imaginary part {0.imag}.'].format[c]
'The complex number [3-5j] is formed from the real part 3.0 and the imaginary part -5.0.'
>>> class Point[object]:
..     def __init__[self, x, y]:
..         self.x, self.y = x, y
..     def __str__[self]:
..         return 'Point[{self.x}, {self.y}]'.format[self=self]
...
>>> str[Point[4, 2]]
'Point[4, 2]'

Truy cập các mục của đối số

>>> coord = [3, 5]
>>> 'X: {0[0]};  Y: {0[1]}'.format[coord]
'X: 3;  Y: 5'

Thay thế

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
11 và
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
12

>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"

Căn chỉnh văn bản và chỉ định chiều rộng

>>> '{:30}'.format['right aligned']
'                 right aligned'
>>> '{:^30}'.format['centered']
'           centered           '
>>> '{:*^30}'.format['centered']  # use '*' as a fill char
'***********centered***********'

Thay thế

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
13,
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
14, và
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
15 và ghi rõ dấu hiệu

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
0

Thay thế

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
16 và
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
17 và chuyển đổi giá trị thành các cơ sở khác nhau

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
1

Sử dụng dấu phẩy làm dấu tách hàng nghìn

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
2

Thể hiện một tỷ lệ phần trăm

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
3

Sử dụng định dạng dành riêng cho loại

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
4

Các đối số lồng nhau và các ví dụ phức tạp hơn

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
5

7. 1. 4. Chuỗi mẫu

Mới trong phiên bản 2. 4

Các mẫu cung cấp các thay thế chuỗi đơn giản hơn như được mô tả trong PEP 292. Thay vì thay thế dựa trên

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
9 bình thường, Mẫu hỗ trợ thay thế dựa trên
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
19, sử dụng các quy tắc sau

  • "Harold's a clever {0!s}"        # Calls str[] on the argument first
    "Bring out the holy {name!r}"    # Calls repr[] on the argument first
    
    20 là một lối thoát;

  • "Harold's a clever {0!s}"        # Calls str[] on the argument first
    "Bring out the holy {name!r}"    # Calls repr[] on the argument first
    
    22 đặt tên cho trình giữ chỗ thay thế khớp với khóa ánh xạ của
    "Harold's a clever {0!s}"        # Calls str[] on the argument first
    "Bring out the holy {name!r}"    # Calls repr[] on the argument first
    
    23. Theo mặc định,
    "Harold's a clever {0!s}"        # Calls str[] on the argument first
    "Bring out the holy {name!r}"    # Calls repr[] on the argument first
    
    23 phải đánh vần mã định danh Python. Ký tự không định danh đầu tiên sau ký tự
    "Harold's a clever {0!s}"        # Calls str[] on the argument first
    "Bring out the holy {name!r}"    # Calls repr[] on the argument first
    
    19 chấm dứt đặc điểm kỹ thuật giữ chỗ này

  • "Harold's a clever {0!s}"        # Calls str[] on the argument first
    "Bring out the holy {name!r}"    # Calls repr[] on the argument first
    
    26 tương đương với
    "Harold's a clever {0!s}"        # Calls str[] on the argument first
    "Bring out the holy {name!r}"    # Calls repr[] on the argument first
    
    22. Nó được yêu cầu khi các ký tự định danh hợp lệ theo sau phần giữ chỗ nhưng không phải là một phần của phần giữ chỗ, chẳng hạn như
    "Harold's a clever {0!s}"        # Calls str[] on the argument first
    "Bring out the holy {name!r}"    # Calls repr[] on the argument first
    
    28

Bất kỳ sự xuất hiện nào khác của

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
19 trong chuỗi sẽ dẫn đến việc tăng

Mô-đun cung cấp một lớp thực hiện các quy tắc này. Các phương pháp là

lớp
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1_______1_______35 [ mẫu ]

Hàm tạo nhận một đối số duy nhất là chuỗi mẫu

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
36 [ ánh xạ [ , **kws< ]]

Thực hiện thay thế mẫu, trả về một chuỗi mới. ánh xạ là bất kỳ đối tượng giống như từ điển nào có khóa khớp với phần giữ chỗ trong mẫu. Ngoài ra, bạn có thể cung cấp các đối số từ khóa, trong đó các từ khóa là phần giữ chỗ. Khi cả ánh xạ và kws được cung cấp và có các bản sao, trình giữ chỗ từ kws được ưu tiên

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
37 [ ánh xạ [ , **kws< ]]

Giống như , ngoại trừ nếu trình giữ chỗ bị thiếu trong ánh xạ và kws, thay vì đưa ra một ngoại lệ, trình giữ chỗ ban đầu sẽ xuất hiện nguyên vẹn trong chuỗi kết quả. Ngoài ra, không giống như với , bất kỳ lần xuất hiện nào khác của

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
19 sẽ chỉ trả về
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
19 thay vì tăng

Mặc dù các ngoại lệ khác vẫn có thể xảy ra, nhưng phương pháp này được gọi là “an toàn” vì nó luôn cố gắng trả về một chuỗi có thể sử dụng được thay vì đưa ra một ngoại lệ. Theo một nghĩa khác, có thể là bất kỳ điều gì khác ngoài an toàn, vì nó sẽ âm thầm bỏ qua các mẫu không đúng định dạng có chứa dấu phân cách lơ lửng, dấu ngoặc nhọn chưa khớp hoặc trình giữ chỗ không phải là mã định danh Python hợp lệ

các phiên bản cũng cung cấp một thuộc tính dữ liệu công khai

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
46

Đây là đối tượng được truyền cho đối số mẫu của hàm tạo. Nói chung, bạn không nên thay đổi nó, nhưng quyền truy cập chỉ đọc không được thực thi

Dưới đây là một ví dụ về cách sử dụng Mẫu

"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
6

Sử dụng nâng cao. bạn có thể lấy các lớp con của để tùy chỉnh cú pháp trình giữ chỗ, ký tự phân cách hoặc toàn bộ biểu thức chính quy được sử dụng để phân tích các chuỗi mẫu. Để làm điều này, bạn có thể ghi đè các thuộc tính lớp này

  • dấu phân cách - Đây là chuỗi ký tự mô tả một trình giữ chỗ giới thiệu dấu phân cách. Giá trị mặc định là

    "Harold's a clever {0!s}"        # Calls str[] on the argument first
    "Bring out the holy {name!r}"    # Calls repr[] on the argument first
    
    19. Lưu ý rằng đây không phải là một biểu thức chính quy, vì việc triển khai sẽ gọi chuỗi này khi cần

  • idpattern – Đây là biểu thức chính quy mô tả mẫu dành cho các trình giữ chỗ không có dấu ngoặc nhọn [dấu ngoặc nhọn sẽ được thêm tự động khi thích hợp]. Giá trị mặc định là biểu thức chính quy

    "Harold's a clever {0!s}"        # Calls str[] on the argument first
    "Bring out the holy {name!r}"    # Calls repr[] on the argument first
    
    50

Ngoài ra, bạn có thể cung cấp toàn bộ mẫu biểu thức chính quy bằng cách ghi đè mẫu thuộc tính lớp. Nếu bạn làm điều này, giá trị phải là một đối tượng biểu thức chính quy với bốn nhóm chụp được đặt tên. Các nhóm chụp tương ứng với các quy tắc được đưa ra ở trên, cùng với quy tắc giữ chỗ không hợp lệ

  • đã thoát - Nhóm này khớp với chuỗi thoát, e. g.

    "Harold's a clever {0!s}"        # Calls str[] on the argument first
    "Bring out the holy {name!r}"    # Calls repr[] on the argument first
    
    20, theo mẫu mặc định

  • có tên – Nhóm này khớp với tên giữ chỗ không có dấu ngoặc;

  • niềng răng - Nhóm này khớp với tên giữ chỗ kèm theo dấu ngoặc nhọn;

  • không hợp lệ - Nhóm này khớp với bất kỳ mẫu dấu phân cách nào khác [thường là một dấu phân cách duy nhất] và nó sẽ xuất hiện cuối cùng trong biểu thức chính quy

7. 1. 5. Hàm chuỗi

Các chức năng sau đây có sẵn để hoạt động trên các đối tượng chuỗi và Unicode. Chúng không có sẵn dưới dạng phương thức chuỗi

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
53 [ s [ , tháng 9 ]]

Tách đối số thành các từ bằng cách sử dụng , viết hoa từng từ bằng cách sử dụng và nối các từ viết hoa bằng cách sử dụng. Nếu đối số thứ hai tùy chọn sep vắng mặt hoặc

>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
0, các ký tự khoảng trắng sẽ được thay thế bằng một khoảng trắng và khoảng trắng ở đầu và cuối sẽ bị xóa, nếu không thì sep được sử dụng để tách và nối các từ

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
59 [ từ, đến ]

Trả về một bảng dịch phù hợp để chuyển đến , bảng này sẽ ánh xạ từng ký tự từ thành ký tự ở cùng một vị trí thành;

Ghi chú

Không sử dụng các chuỗi bắt nguồn từ và làm đối số; . Đối với chuyển đổi trường hợp, hãy luôn sử dụng và

7. 1. 6. Hàm chuỗi không dùng nữa

Danh sách các hàm sau đây cũng được định nghĩa là các phương thức của các đối tượng chuỗi và Unicode; . Bạn nên coi các chức năng này là không dùng nữa, mặc dù chúng sẽ không bị xóa cho đến Python 3. Các chức năng được xác định trong mô-đun này là

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
66 [ s ]

Không dùng nữa kể từ phiên bản 2. 0. Sử dụng hàm có sẵn.

Chuyển đổi một chuỗi thành một số dấu phẩy động. Chuỗi phải có cú pháp tiêu chuẩn cho một ký tự dấu chấm động trong Python, tùy ý bắt đầu bằng một dấu [

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
68 hoặc
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
69]. Lưu ý rằng điều này hoạt động giống hệt với chức năng tích hợp khi truyền một chuỗi

Ghi chú

Khi truyền vào một chuỗi, các giá trị cho NaN và Infinity có thể được trả về, tùy thuộc vào thư viện C bên dưới. Tập hợp chuỗi cụ thể được chấp nhận khiến các giá trị này được trả về phụ thuộc hoàn toàn vào thư viện C và được biết là thay đổi

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
72 [ s [ , cơ sở ]]

Không dùng nữa kể từ phiên bản 2. 0. Sử dụng hàm có sẵn.

Chuyển đổi chuỗi s thành một số nguyên trong cơ số đã cho. Chuỗi phải bao gồm một hoặc nhiều chữ số, tùy chọn đứng trước một dấu hiệu [

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
68 hoặc
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
69]. Cơ sở mặc định là 10. Nếu là 0, cơ sở mặc định được chọn tùy thuộc vào các ký tự đầu của chuỗi [sau khi bỏ dấu].
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
76 hoặc
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
77 nghĩa là 16,
"First, thou shalt count to {0}"  # References first positional argument
"Bring me a {}"                   # Implicitly references the first positional argument
"From {} to {}"                   # Same as "From {0} to {1}"
"My quest is {name}"              # References keyword argument 'name'
"Weight in tons {0.weight}"       # 'weight' attribute of first positional arg
"Units destroyed: {players[0]}"   # First element of keyword argument 'players'.
89 nghĩa là 8, bất kỳ thứ gì khác nghĩa là 10. Nếu cơ sở là 16, thì một
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
76 hoặc
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
77 ở đầu luôn được chấp nhận, mặc dù không bắt buộc. Điều này hoạt động giống hệt với chức năng tích hợp khi truyền một chuỗi. [Cũng lưu ý. để giải thích linh hoạt hơn các chữ số, hãy sử dụng hàm tích hợp. ]

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
84 [ s [ , cơ sở ]]

Không dùng nữa kể từ phiên bản 2. 0. Sử dụng hàm có sẵn.

Chuyển đổi chuỗi s thành một số nguyên dài trong cơ số đã cho. Chuỗi phải bao gồm một hoặc nhiều chữ số, tùy chọn đứng trước một dấu hiệu [

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
68 hoặc
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
69]. Đối số cơ sở có cùng ý nghĩa như đối với. Không được phép sử dụng dấu sau
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
89 hoặc
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
90, trừ khi cơ số là 0. Lưu ý rằng khi được gọi mà không có cơ sở hoặc với cơ sở được đặt thành 10, hàm này hoạt động giống hệt với hàm tích hợp khi truyền một chuỗi

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
93 [ từ ]

Trả về một bản sao của từ chỉ có ký tự đầu tiên được viết hoa

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
95 [ s [ , kích thước tab ]]

Mở rộng các tab trong một chuỗi thay thế chúng bằng một hoặc nhiều dấu cách, tùy thuộc vào cột hiện tại và kích thước tab đã cho. Số cột được đặt lại về 0 sau mỗi dòng mới xuất hiện trong chuỗi. Điều này không hiểu các ký tự không in khác hoặc các chuỗi thoát. Kích thước tab mặc định là 8

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
97 [ s, phụ [ , bắt đầu< [, end]]]

Trả về chỉ số thấp nhất trong s nơi tìm thấy chuỗi con sub sao cho sub được chứa hoàn toàn trong

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
98. Trả lại
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
99 khi thất bại. Mặc định cho bắt đầu và kết thúc và giải thích các giá trị âm cũng giống như đối với các lát cắt

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
01 [ s, phụ [ , bắt đầu< [, end]]]

Like nhưng tìm chỉ số cao nhất

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
04 [ s, phụ [ , bắt đầu< [, end]]]

Thích nhưng tăng khi không tìm thấy chuỗi con

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
08 [ s, phụ [ , bắt đầu< [, end]]]

Thích nhưng tăng khi không tìm thấy chuỗi con

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
12 [ s, phụ [ , bắt đầu< [, end]]]

Trả về số lần xuất hiện [không trùng lặp] của chuỗi con sub trong chuỗi

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
98. Mặc định cho bắt đầu và kết thúc và giải thích các giá trị âm cũng giống như đối với các lát cắt

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
15 [ s ]

Trả về một bản sao của s, nhưng với chữ hoa được chuyển thành chữ thường

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
17 [ s [ , tháng 9 [, maxsplit]]]

Trả về danh sách các từ của chuỗi s. Nếu không có đối số thứ hai tùy chọn sep hoặc

>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
0, thì các từ được phân tách bằng các chuỗi ký tự khoảng trắng tùy ý [dấu cách, tab, dòng mới, trả về, nguồn cấp dữ liệu]. Nếu đối số thứ hai sep xuất hiện và không phải là
>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
0, thì nó chỉ định một chuỗi được sử dụng làm dấu tách từ. Sau đó, danh sách được trả về sẽ có thêm một mục so với số lần xuất hiện không chồng lấp của dấu phân cách trong chuỗi. Nếu maxsplit được đưa ra, thì tối đa số lần tách maxsplit xảy ra và phần còn lại của chuỗi được trả về dưới dạng phần tử cuối cùng của danh sách [do đó, danh sách sẽ có tối đa
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
20 phần tử]. Nếu maxsplit không được chỉ định hoặc
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
99, thì không có giới hạn về số lần phân tách [tất cả các lần phân tách có thể được thực hiện]

Hành vi phân tách trên một chuỗi rỗng phụ thuộc vào giá trị của sep. Nếu sep không được chỉ định hoặc được chỉ định là

>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
0, kết quả sẽ là một danh sách trống. Nếu sep được chỉ định là một chuỗi bất kỳ, kết quả sẽ là một danh sách chứa một phần tử là một chuỗi rỗng

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
24 [ s [ , tháng 9 [, maxsplit]]]

Trả về danh sách các từ của chuỗi s, quét s từ cuối. Đối với tất cả ý định và mục đích, danh sách các từ kết quả giống như được trả về bởi , ngoại trừ khi đối số thứ ba tùy chọn maxsplit được chỉ định rõ ràng và khác không. Nếu maxsplit được đưa ra, thì tối đa số lần tách maxsplit - số lần tách ngoài cùng bên phải - xảy ra và phần còn lại của chuỗi được trả về dưới dạng phần tử đầu tiên của danh sách [do đó, danh sách sẽ có tối đa __8_______20 phần tử]

Mới trong phiên bản 2. 4

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
28 [ s [ , tháng 9 [, maxsplit]]]

Chức năng này hoạt động giống hệt với. [Trước đây, chỉ dùng với một đối số, while chỉ dùng với hai đối số. ]

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
33 [ từ [ , tháng 9 ]]

Nối một danh sách hoặc bộ từ với các lần xuất hiện xen kẽ của sep. Giá trị mặc định cho sep là một ký tự khoảng trắng. Luôn luôn đúng rằng

format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
34 bằng s

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
36 [ từ [ , tháng 9 ]]

Chức năng này hoạt động giống hệt với. [Trước đây, chỉ dùng với một đối số, while chỉ dùng với hai đối số. ] Lưu ý rằng không có phương thức nào trên các đối tượng chuỗi;

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
43 [ s [ , ký tự ]]

Trả về một bản sao của chuỗi đã xóa các ký tự đầu. Nếu ký tự bị bỏ qua hoặc

>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
0, ký tự khoảng trắng sẽ bị xóa. Nếu được cung cấp và không phải là
>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
0, ký tự phải là một chuỗi;

Đã thay đổi trong phiên bản 2. 2. 3. Tham số ký tự đã được thêm vào. Tham số ký tự không thể được chuyển vào trước đó 2. 2 phiên bản.

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
47 [ s [ , ký tự ]]

Trả về một bản sao của chuỗi đã xóa các ký tự ở cuối. Nếu ký tự bị bỏ qua hoặc

>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
0, ký tự khoảng trắng sẽ bị xóa. Nếu được cung cấp và không phải là
>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
0, ký tự phải là một chuỗi;

Đã thay đổi trong phiên bản 2. 2. 3. Tham số ký tự đã được thêm vào. Tham số ký tự không thể được chuyển vào trước đó 2. 2 phiên bản.

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
51 [ s [ , ký tự ]]

Trả về một bản sao của chuỗi đã xóa các ký tự đầu và cuối. Nếu ký tự bị bỏ qua hoặc

>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
0, ký tự khoảng trắng sẽ bị xóa. Nếu được cung cấp và không phải là
>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
0, ký tự phải là một chuỗi;

Đã thay đổi trong phiên bản 2. 2. 3. Tham số ký tự đã được thêm vào. Tham số ký tự không thể được chuyển vào trước đó 2. 2 phiên bản.

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
55 [ s ]

Trả về một bản sao của s, nhưng chữ thường được chuyển thành chữ hoa và ngược lại

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
57 [ s, bảng [ , xóa ký tự< ]]

Xóa tất cả các ký tự khỏi s có trong ký tự xóa [nếu có], sau đó dịch các ký tự bằng cách sử dụng bảng, phải là một chuỗi 256 ký tự cung cấp bản dịch cho từng giá trị ký tự, được lập chỉ mục theo thứ tự của nó. Nếu bảng là

>>> "repr[] shows quotes: {!r}; str[] doesn't: {!s}".format['test1', 'test2']
"repr[] shows quotes: 'test1'; str[] doesn't: test2"
0 thì chỉ thực hiện bước xóa ký tự

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
60 [ s ]

Trả về một bản sao của s, nhưng với các chữ thường được chuyển thành chữ hoa

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
62 [ s, chiều rộng [ , fillchar< ]]
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
64[s, width[, fillchar]]
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
66[s, width[, fillchar]]

Các hàm này lần lượt căn trái, căn phải và căn giữa một chuỗi trong một trường có chiều rộng nhất định. Chúng trả về một chuỗi có chiều rộng ít nhất là các ký tự chiều rộng, được tạo bằng cách đệm chuỗi s bằng ký tự fillchar [mặc định là khoảng trắng] cho đến chiều rộng đã cho ở bên phải, bên trái hoặc cả hai bên. Chuỗi không bao giờ bị cắt ngắn

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
68 [ s, chiều rộng ]

Đệm một chuỗi số s ở bên trái với các chữ số 0 cho đến khi đạt được chiều rộng đã cho. Các chuỗi bắt đầu bằng một dấu hiệu được xử lý chính xác

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
1
format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]
fill        ::=  
align       ::=  "" | "=" | "^"
sign        ::=  "+" | "-" | " "
width       ::=  integer
precision   ::=  integer
type        ::=  "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
70 [ s, cũ, mới [ , ]]

Trả về một bản sao của chuỗi s với tất cả các lần xuất hiện của chuỗi con cũ được thay thế bằng chuỗi mới. Nếu đối số tùy chọn maxreplace được đưa ra, thì lần xuất hiện maxreplace đầu tiên sẽ được thay thế

Chủ Đề