Trong python, chuỗi là…
Nắm vững kiến thức cơ bản về phân tích dữ liệu với Python chỉ trong bốn giờ. Khóa học trực tuyến này sẽ giới thiệu giao diện Python và khám phá các gói phổ biến
Xem chi tiết Mũi tên phải Show Python trung gianNgười bắt đầu4 giờ 890. 7k Nâng cao kỹ năng khoa học dữ liệu của bạn bằng cách tạo trực quan hóa bằng Matplotlib và thao tác với DataFrames bằng gấu trúcNối các hằng 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'.8 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'.9 được mô tả bên dưới. Giá trị này không phụ thuộc vào ngôn ngữchuỗi. ascii_lowercase ¶ 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 "More {!a}" # Calls ascii() on the argument first0. Giá trị này không phụ thuộc vào ngôn ngữ và sẽ không thay đổichuỗi. ascii_uppercase ¶ Các 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 "More {!a}" # Calls ascii() on the argument first1. Giá trị này không phụ thuộc vào ngôn ngữ và sẽ không thay đổichuỗi. chữ số ¶ 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 "More {!a}" # Calls ascii() on the argument first2chuỗi. chữ số thập lục phân ¶ 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 "More {!a}" # Calls ascii() on the argument first3chuỗi. chữ số bát phân ¶ 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 "More {!a}" # Calls ascii() on the argument first4chuỗi. dấu câu ¶ 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 "More {!a}" # Calls ascii() on the argument first5. "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first6chuỗi. có thể in được ¶ Chuỗi ký tự ASCII đượ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 "More {!a}" # Calls ascii() on the argument first7, "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first8, "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first9 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'.20chuỗi. khoảng trắng ¶ Một chuỗi chứa tất cả các ký tự ASCII được coi là khoảng trắ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 Định dạng chuỗi tùy chỉnh¶Lớp chuỗi dựng sẵn 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 "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 mô tả trong PEP 3101. Lớp "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'.22 trong mô-đun "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'.7 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 "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 tích hợplớp chuỗi. Trình định dạng ¶ Lớp "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'.22 có các phương thức công khai sauđịnh dạng(chuỗi_định dạng , /, *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 "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'.26 Đã thay đổi trong phiên bản 3. 7. Đối số chuỗi định dạng giờ đây là chỉ dành cho vị trí . vformat(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 "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'.27 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'.28. "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'.26 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, "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'.22 định nghĩa một số phương thức được dự định thay thế bởi các lớp conphân tích cú pháp(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 "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'.26 để 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 "More {!a}" # Calls ascii() on the argument first42get_field(field_name , args, kwargs)¶ Đã cho field_name như đượ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 "More {!a}" # Calls ascii() on the argument first43 (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 "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'.26. 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 "More {!a}" # Calls ascii() on the argument first45get_value(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 "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'.26 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 "More {!a}" # Calls ascii() on the argument first45 đượ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 "More {!a}" # Calls ascii() on the argument first48 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 "More {!a}" # Calls ascii() on the argument first45 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 "More {!a}" # Calls ascii() on the argument first20 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 "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first21 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 "More {!a}" # Calls ascii() on the argument first22check_unused_args(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 "More {!a}" # Calls ascii() on the argument first23 được cho là đưa ra một ngoại lệ nếu kiểm tra không thành côngformat_field(giá trị , format_spec)¶ "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first24 chỉ cần gọi toàn cầ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'.21 tích hợp. Phương thức được cung cấp để các lớp con có thể ghi đè lên nóconvert_field(giá trị , chuyển đổi)¶ Chuyển đổi giá trị (đượ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 "More {!a}" # Calls ascii() on the argument first26) được cung cấp một loại chuyển đổi (như trong bộ được trả về bởi 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 "More {!a}" # Calls ascii() on the argument first43). Phiên bản mặc định hiểu các loại chuyển đổi 's' (str), 'r' (repr) và 'a' (ascii) Cú pháp chuỗi định dạng¶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 "More {!a}" # Calls ascii() on the argument first28 và lớp "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'.22 chia sẻ cùng một cú pháp cho chuỗi định dạng (mặc dù trong trường hợp 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'.22, các lớp con có thể xác định cú pháp chuỗi định dạng của riêng chúng). Cú pháp có liên quan đến cú pháp của các chuỗi ký tự được định dạng , nhưng nó kém phức tạp hơn và đặc biệt là không hỗ trợ các biểu thức tùy ý. Chuỗi định dạng chứa “trường thay thế” được bao quanh bởi dấu ngoặc nhọ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 "More {!a}" # Calls ascii() on the argument first31. 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. "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first32 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 "More {!a}" # Calls ascii() on the argument first33 Ngữ pháp cho trường thay thế như 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 "More {!a}" # Calls ascii() on the argument first6 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 "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first34 và định dạng_spec, đứng trước dấu hai chấ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 "More {!a}" # Calls ascii() on the argument first35. Chúng chỉ định định dạng không mặc định cho giá trị thay thế Xem thêm phần Ngôn ngữ nhỏ đặc tả định dạng . 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 "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first36 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 "More {!a}" # Calls ascii() on the argument first37) 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 "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first38 chọn thuộc tính được đặt tên bằng cách sử dụ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 "More {!a}" # Calls ascii() on the argument first20, trong khi một biểu thức có dạ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 "More {!a}" # Calls ascii() on the argument first30 thực hiện tra cứu chỉ mục bằng cách sử dụ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 "More {!a}" # Calls ascii() on the argument first31 Đã thay đổi trong phiên bản 3. 1. Có thể bỏ qua các chỉ định đối số vị trí cho "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first28, do đó, "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first33 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 "More {!a}" # Calls ascii() on the argument first34. Đã thay đổi trong phiên bản 3. 4. Có thể bỏ qua các chỉ định đối số vị trí 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'.22. 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 "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first36 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 chuỗi trước khi 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 "More {!a}" # Calls ascii() on the argument first36, logic định dạng thông thường được bỏ qua Ba cờ chuyển đổi hiện được hỗ trợ. "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first38 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 "More {!a}" # Calls ascii() on the argument first39 trên giá trị, "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first40 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 "More {!a}" # Calls ascii() on the argument first41 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 "More {!a}" # Calls ascii() on the argument first42 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 "More {!a}" # Calls ascii() on the argument first43 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 "More {!a}" # Calls ascii() 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 Ví dụ về định dạng để biết một số ví dụ. Đặc tả định dạng Ngôn ngữ nhỏ¶"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 Cú pháp chuỗi định dạng và . Chúng cũng có thể được chuyển trực tiếp đến chức nă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'.21 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. Formatted string literals). They can also be passed directly to the built-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'.21 function. Each formattable type may define how the format specification is to be interpreted. 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 đặc tả định dạng trống sẽ tạo ra kết quả giống như khi bạn đã 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 "More {!a}" # Calls ascii() on the argument first39 trên giá trị. Đặc tả đị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à "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 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 chữ (” "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first46” 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 "More {!a}" # Calls ascii() on the argument first47”) làm ký tự điền trong chuỗi ký tự được định dạng hoặc khi sử dụng . Tuy nhiên, có thể chèn dấu ngoặc nhọn với trường thay thế lồng nhau. Giới hạn này không ảnh hưởng đến hà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'.21. Ý nghĩa của các tùy chọn căn chỉnh khác nhau như sau
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
Tùy chọ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 "More {!a}" # Calls ascii() on the argument first66 ép buộc các giá trị dấu phẩy động 0 âm thành 0 dương sau khi làm tròn đến độ chính xác của định dạng. Tùy chọn này chỉ hợp lệ cho các kiểu trình bày dấu phẩy động Đã thay đổi trong phiên bản 3. 11. Đã thêm tùy chọ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 "More {!a}" # Calls ascii() on the argument first66 (xem thêm PEP 682). Tùy chọ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 "More {!a}" # Calls ascii() on the argument first68 khiến "dạng thay thế" được sử dụng để chuyển đổi. Hình thức thay thế được xác định khác nhau cho các loại khác nhau. Tùy chọn này chỉ hợp lệ cho các loại số nguyên, float và phức tạp. Đối với số nguyên, khi đầu ra nhị phân, bát phân hoặc thập lục phân được sử dụng, tùy chọn này sẽ thêm tiền tố tương ứ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 "More {!a}" # Calls ascii() on the argument first69, "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'.00, "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'.01 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'.02 vào giá trị đầu ra. Đối với float và phức, dạng thay thế khiến kết quả chuyển đổi luôn chứa ký tự dấu thập phân, ngay cả khi không có chữ số nào theo sau nó. Thông thường, một ký tự dấu thập phân chỉ xuất hiện trong kết quả của những chuyển đổi này nếu một chữ số theo sau nó. Ngoài ra, đối với chuyể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'.03 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'.04, các số 0 ở cuối không bị xóa khỏi kết quả 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'.05 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 tá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'.06 Đã thay đổi trong phiên bản 3. 1. Đã 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'.05 (xem thêm PEP 378). 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'.08 báo hiệu việc sử dụng dấu gạch dưới cho dấu phân cách hàng nghìn đối với kiểu trình bày dấu phẩy động và 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'.09. Đối với các 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'.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'.11, "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'.12 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'.13, dấu gạch dưới sẽ được chèn sau mỗi 4 chữ số. Đối với các loại bản trình bày khác, việc chỉ định tùy chọn này là một lỗi Đã thay đổi trong phiên bản 3. 6. Đã 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'.08 (xem thêm PEP 515). chiều rộng là một số nguyên thập phân xác định tổng chiều rộng trường tối thiểu, bao gồm mọi tiền tố, dấu phân cách và các ký tự định dạng khác. 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ó căn chỉnh rõ ràng nào được đưa ra, trước trường chiều rộng là 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'.15) 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'.15 với kiểu căn chỉ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 "More {!a}" # Calls ascii() on the argument first62 Đã thay đổi trong phiên bản 3. 10. Đặt trước trường chiều rộ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'.15 không còn ảnh hưởng đến căn chỉnh mặc định cho chuỗi. Độ chính xác là một số nguyên 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 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'.19 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'.20 hoặc trước và sau dấu thập phân đối 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'.03 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'.04. Đối với các kiểu trình bày chuỗi, 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 đối với các kiểu trình bày 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à
Các kiểu trình bày số nguyên có sẵn là
Ngoài các kiểu trình bày ở trên, các 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'.06 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 "More {!a}" # Calls ascii() on the argument first42). Khi làm như 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'.39 đượ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 các giá 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'.40 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'.41 là
Ví dụ về định dạng¶Phần này chứa các ví dụ về 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 "More {!a}" # Calls ascii() on the argument first28 và so sánh với định dạ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 "More {!a}" # Calls ascii() on the argument first05 cũ Trong hầu hết các trường hợp, cú pháp tương tự như định dạ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 "More {!a}" # Calls ascii() on the argument first05 cũ, với việc bổ sung "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first31 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 "More {!a}" # Calls ascii() on the argument first08 được sử dụng thay 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 "More {!a}" # Calls ascii() on the argument first05. 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 "More {!a}" # Calls ascii() on the argument first10 có thể được dịch sang "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first11 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í "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first4 Truy cập đối số theo tê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 "More {!a}" # Calls ascii() on the argument first2 Truy cập các thuộc tính của đố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 "More {!a}" # Calls ascii() on the argument first3 Truy cập các mục của đố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 "More {!a}" # Calls ascii() on the argument first3 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 "More {!a}" # Calls ascii() on the argument first12 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 "More {!a}" # Calls ascii() on the argument first13 "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first4 Căn chỉnh văn bản và chỉ định chiều 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 "More {!a}" # Calls ascii() on the argument first6 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 "More {!a}" # Calls ascii() on the argument first14, "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first15 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 "More {!a}" # Calls ascii() on the argument first16 và chỉ định một 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 "More {!a}" # Calls ascii() on the argument first17 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 "More {!a}" # Calls ascii() on the argument first18 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 Chuỗi mẫu¶Các chuỗi mẫu cung cấp các thay thế chuỗi đơn giản hơn như được mô tả trong PEP 292. Trường hợp sử dụng chính cho chuỗi mẫu là để quốc tế hóa (i18n) vì trong ngữ cảnh đó, cú pháp và chức năng đơn giản hơn giúp dịch dễ dàng hơn các phương tiện định dạng chuỗi tích hợp sẵn khác trong Python. Để làm ví dụ về thư viện được xây dựng trên các chuỗi mẫu cho i18n, hãy xem lưu lượng. gói i18n Chuỗi 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 "More {!a}" # Calls ascii() on the argument first19, sử dụng các quy tắc sau
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 "More {!a}" # Calls ascii() on the argument first19 trong chuỗi sẽ dẫn đến việc 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 "More {!a}" # Calls ascii() on the argument first30 Mô-đun "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'.7 cung cấ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 "More {!a}" # Calls ascii() on the argument first32 thực hiện các quy tắc này. Các phương phá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 "More {!a}" # Calls ascii() on the argument first32 làlớp chuỗi. Mẫu(mẫu) ¶ Hàm tạo nhận một đối số duy nhất là chuỗi mẫu thay thế(ánh xạ={}, /, **kwds)¶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à kwds được cung cấp và có các bản sao, trình giữ chỗ từ kwds được ưu tiên safe_substitute(ánh xạ={}, /, **kwds)¶Giống 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 "More {!a}" # Calls ascii() on the argument first34, ngoại trừ nếu trình giữ chỗ bị thiếu trong ánh xạ và kwds, thay vì đưa ra ngoại 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 "More {!a}" # Calls ascii() on the argument first22, 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 "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first34, 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 "More {!a}" # Calls ascii() on the argument first19 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 "More {!a}" # Calls ascii() on the argument first19 thay vì 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 "More {!a}" # Calls ascii() on the argument first30 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, "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first40 có thể là bất kỳ thứ 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 không khớp hoặc trình giữ chỗ không phải là mã định danh Python hợp lệis_valid() ¶ Trả về false nếu mẫu có trình giữ chỗ không hợp lệ 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 "More {!a}" # Calls ascii() on the argument first34 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 "More {!a}" # Calls ascii() on the argument first30 Mới trong phiên bản 3. 11 get_identifiers() ¶Trả về danh sách các số nhận dạng hợp lệ trong mẫu, theo thứ tự chúng xuất hiện lần đầu, bỏ qua mọi số nhận dạng không hợp lệ Mới trong phiên bản 3. 11 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 "More {!a}" # Calls ascii() on the argument first32 cũng cung cấp một thuộc tính dữ liệu công khaimẫu ¶ Đâ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 "Harold's a clever {0!s}" # Calls str() on the argument first "Bring out the holy {name!r}" # Calls repr() on the argument first "More {!a}" # Calls ascii() on the argument first32 để 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
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ệ
Các phương thức trên lớp này sẽ 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 "More {!a}" # Calls ascii() on the argument first30 nếu mẫu khớp với mẫu mà không có một trong các nhóm được đặt tên này khớp Hàm trợ giúp¶chuỗi. capwords(s , sep=None)¶Chia đối số thành các từ bằng cách sử dụ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 "More {!a}" # Calls ascii() on the argument first57, viết hoa từng từ bằng cách sử dụ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 "More {!a}" # Calls ascii() on the argument first58 và nối các từ viết hoa bằng cách sử dụ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 "More {!a}" # Calls ascii() on the argument first59. Nếu đối số thứ hai tùy chọn sep không có 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 "More {!a}" # Calls ascii() on the argument first42, 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ừ Các chuỗi có thể thay đổi trong Python không?Chuỗi không thể thay đổi trong Python . Chuỗi là một kiểu dữ liệu không thể thay đổi, có nghĩa là giá trị của nó không thể được cập nhật.
Loại chuỗi nào trong Python?Chuỗi là chuỗi dữ liệu ký tự . Kiểu chuỗi trong Python được gọi là str.
Là đối tượng chuỗi trong Python?Chuỗi là đối tượng trong Python có nghĩa là có một tập hợp các hàm dựng sẵn mà bạn có thể sử dụng để thao tác với chuỗi. Bạn sử dụng ký hiệu dấu chấm để gọi các hàm trên một đối tượng chuỗi chẳng hạn như câu.
Là các biến chuỗi trong Python?Loạt bài hướng dẫn. Làm việc với Chuỗi trong Python 3
. Được tạo thành từ Unicode, các chuỗi là các chuỗi bất biến, có nghĩa là chúng không thay đổi. can be either a constant or a variable. Made up of Unicode, strings are immutable sequences, meaning they are unchanging. |