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 first3 và
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first4 đượ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 first1______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 first7. 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 first1
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first9¶
Các chữ hoa
format_spec ::= [[0. Giá trị này không phụ thuộc vào ngôn ngữ và sẽ không thay đổifill
]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" | "%"
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first1______72¶
Chuỗi
format_spec ::= [[3fill
]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" | "%"
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first1______75¶
Chuỗi
format_spec ::= [[6fill
]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" | "%"
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first1______78¶
Sự nối của các chuỗi
format_spec ::= [[9 và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" | "%"
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first70 đượ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 first71 đượ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 first1______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 first7. 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 first71 đượ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 first1____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 first78
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first1____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 first71
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first1____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 first74,
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first75,
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first76 và
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first77
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first1______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 ::= [[0. Giá trị cụ thể phụ thuộc vào ngôn ngữ và sẽ được cập nhật khifill
]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" | "%"
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first71 đượ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 first1______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 first04 đượ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 first05 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 first37 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 first07 tích hợplớp
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first1____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 first05 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 first81 [ 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 first82
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first83 [ 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 first84 và
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first85.
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first82 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 first05 đị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 first88 [ 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 first82 để 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 first70
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first71 [ 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 first72 [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 first82. 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 first74
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first75 [ 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 first82 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 first74 đượ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 first78 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 first74 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 first70 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 first71 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 first72
"Harold's a clever {0!s}" # Calls str[] on the argument first "Bring out the holy {name!r}" # Calls repr[] on the argument first73 [ 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 first74 đượ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 first75 [ value, format_spec ] ¶