Html bên trong chuỗi

Nối các hằ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
3 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
4 được mô tả bên dưới. 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______06¶

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

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______72¶

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______75¶

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______78¶

Sự nối của các 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" | "%"
9 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
70 được mô tả bên dưới. Giá trị cụ thể phụ thuộc vào ngôn ngữ và sẽ được cập nhật khi
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
71 đượ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______173¶

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
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
71 đượ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____177¶

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
78

"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____270¶

Chuỗi ký tự ASCII được coi là ký tự dấu chấm câu trong 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
71

"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____273¶

Chuỗi ký tự được coi là có thể in được. Đây là sự kết hợp 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
74,
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
75,
"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 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
77

"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______279¶

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
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
71 đượ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______403¶

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

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
04 được mô tả trong PEP 3101. 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
05 trong mô-đun
"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 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
"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 tích hợp

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____409¶

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
05 có các phương thức công khai 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
81 [ 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

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

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
83 [ 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

"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 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
85.
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
82 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,

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
05 định nghĩa một số phương thức được dự định thay thế bởi các lớp 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
88 [ 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 bở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
82 để ngắt chuỗi thành văn bản bằng chữ hoặc cá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à

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

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

Tên trường đã cho được trả về bở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
72 [xem bên 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. Tiêu đề". args và kwargs được chuyển vào
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
82. Giá trị trả về used_key có cùng ý nghĩa với tham số chính 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
74

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
75 [ 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

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
82 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ẽ khiế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
74 được gọi với đối số chính là 0. Thuộc tí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
78 sẽ được tra cứu sau khi
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
74 trả về bằng cách gọi hàm
"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
70 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ì phải tă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
71 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
72

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
73 [ 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.

"Harold's a clever {0!s}"        # Calls str[] on the argument first
"Bring out the holy {name!r}"    # Calls repr[] on the argument first
74 được cho là đưa ra một ngoại lệ nếu kiểm tra không thành cô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
75 [ value, format_spec ]

Chủ Đề