Định dạng chuỗi là quá trình truyền các thứ trong chuỗi một cách linh hoạt và trình bày chuỗi.
Có bốn cách khác nhau để thực hiện định dạng chuỗi trong Python
- Định dạng với toán tử %
- Định dạng bằng phương thức chuỗi format[]
- Định dạng bằng chuỗi ký tự, được gọi là chuỗi f
- Định dạng với Lớp mẫu chuỗi
Vì vậy, chúng ta sẽ xem toàn bộ các cách được đề cập ở trên và chúng ta cũng sẽ tập trung vào chiến lược định dạng chuỗi nào là tốt nhất
Phương pháp 1. Định dạng chuỗi sử dụng toán tử %
Đây là phương pháp định dạng chuỗi lâu đời nhất. Ở đây chúng tôi sử dụng toán tử modulo %. Modulo % còn được gọi là “toán tử định dạng chuỗi”
ví dụ 1. Định dạng chuỗi bằng toán tử %
Python3
Misha walked and looked around.5
Misha walked and looked around.6
Misha walked and looked around.7
Misha walked and looked around.8
Misha walked and looked around.9
Joe stood up and spoke to the crowd. There are 4 dogs.0
Joe stood up and spoke to the crowd. There are 4 dogs.1____52
Joe stood up and spoke to the crowd. There are 4 dogs.3
đầu ra
The mangy, scrawny stray dog hurriedly gobbled down the grain-free, organic dog food.
Bạn cũng có thể chèn nhiều chuỗi cùng một lúc và cũng có thể sử dụng các biến để chèn các đối tượng vào chuỗi
ví dụ 2. Tiêm nhiều chuỗi bằng toán tử %
Python3
Joe stood up and spoke to the crowd. There are 4 dogs.4
Joe stood up and spoke to the crowd. There are 4 dogs.5
Joe stood up and spoke to the crowd. There are 4 dogs.6
Misha walked and looked around.5
Misha walked and looked around.6
Joe stood up and spoke to the crowd. There are 4 dogs.9
Joe stood up and spoke to the crowd. There are 4 dogs.1
Misha walked and looked around.6
Misha walked and looked around.82
Misha walked and looked around.83
đầu ra
Misha walked and looked around.
'%s' được sử dụng để chèn chuỗi tương tự '%d' cho số nguyên, '%f' cho giá trị dấu phẩy động, '%b' cho định dạng nhị phân. Đối với tất cả các định dạng, phương thức chuyển đổi, hãy truy cập tài liệu chính thức
ví dụ 3
Python3
Misha walked and looked around.5
Misha walked and looked around.6
Misha walked and looked around.86
Joe stood up and spoke to the crowd. There are 4 dogs.1
Misha walked and looked around.88
Joe stood up and spoke to the crowd. There are 4 dogs.3
Misha walked and looked around.5
Misha walked and looked around.6
Misha walked and looked around.92
Joe stood up and spoke to the crowd. There are 4 dogs.1
Misha walked and looked around.94
Joe stood up and spoke to the crowd. There are 4 dogs.3
đầu ra
Joe stood up and spoke to the crowd. There are 4 dogs.
Độ chính xác nổi với phương pháp giữ chỗ
Số dấu phẩy động sử dụng định dạng %a. bf. Ở đây, a sẽ là số chữ số tối thiểu có trong chuỗi; . Gần với điều này, bf biểu thị có bao nhiêu chữ số sẽ được hiển thị sau dấu thập phân.
Hãy để chúng tôi xem một vài ví dụ
ví dụ 1. Độ chính xác của dấu phẩy động sử dụng toán tử %
Python3
Misha walked and looked around.5
Misha walked and looked around.6
Misha walked and looked around.98
Joe stood up and spoke to the crowd. There are 4 dogs.1
Misha walked and looked around.6
Misha walked and looked around.91
Misha walked and looked around.92
đầu ra
Misha walked and looked around.8
ví dụ 2
Python3
Misha walked and looked around.5
Misha walked and looked around.6
Misha walked and looked around.95
Joe stood up and spoke to the crowd. There are 4 dogs.1
Misha walked and looked around.6
Misha walked and looked around.98
Misha walked and looked around.92
đầu ra
Misha walked and looked around.9
ví dụ 3. Bạn có thể sử dụng nhiều loại chuyển đổi định dạng trong một câu lệnh in
Python3
Joe stood up and spoke to the crowd. There are 4 dogs.00
Joe stood up and spoke to the crowd. There are 4 dogs.5
Joe stood up and spoke to the crowd. There are 4 dogs.02
Joe stood up and spoke to the crowd. There are 4 dogs.03
Joe stood up and spoke to the crowd. There are 4 dogs.5
Joe stood up and spoke to the crowd. There are 4 dogs.05
Joe stood up and spoke to the crowd. There are 4 dogs.5
Joe stood up and spoke to the crowd. There are 4 dogs.1
Joe stood up and spoke to the crowd. There are 4 dogs.08
Joe stood up and spoke to the crowd. There are 4 dogs.09
Joe stood up and spoke to the crowd. There are 4 dogs.00
Joe stood up and spoke to the crowd. There are 4 dogs.5
Joe stood up and spoke to the crowd. There are 4 dogs.1
Joe stood up and spoke to the crowd. There are 4 dogs.03
Joe stood up and spoke to the crowd. There are 4 dogs.1
Joe stood up and spoke to the crowd. There are 4 dogs.05
Misha walked and looked around.5
Joe stood up and spoke to the crowd. There are 4 dogs.07
Đầu ra
Misha walked and looked around.9
Ghi chú. Để biết thêm về định dạng %, hãy tham khảo Định dạng chuỗi trong Python bằng cách sử dụng %
Phương pháp 2. Định dạng chuỗi bằng phương thức format[]
Phương thức Format[] đã được giới thiệu với Python3 để xử lý định dạng chuỗi phức tạp hiệu quả hơn. Trình định dạng hoạt động bằng cách đặt một hoặc nhiều trường thay thế và trình giữ chỗ được xác định bởi một cặp dấu ngoặc nhọn { } vào một chuỗi và gọi phương thức str. định dạng[]. Giá trị chúng tôi muốn đặt vào trình giữ chỗ và nối với chuỗi được truyền dưới dạng tham số vào hàm định dạng.
cú pháp. 'Chuỗi ở đây {} thì cũng {}'. định dạng ['cái gì đó1′,'cái gì đó2']
Ví dụ. Định dạng chuỗi bằng phương thức format[]
Python3
Misha walked and looked around.5
Misha walked and looked around.6
Joe stood up and spoke to the crowd. There are 4 dogs.10
Joe stood up and spoke to the crowd. There are 4 dogs.11
Joe stood up and spoke to the crowd. There are 4 dogs.12
Misha walked and looked around.6
Joe stood up and spoke to the crowd. There are 4 dogs.14
Misha walked and looked around.92
đầu ra
Joe stood up and spoke to the crowd. There are 4 dogs.0
Các. format[] có nhiều ưu điểm hơn so với phương thức placeholder
- Chúng ta có thể chèn đối tượng bằng cách sử dụng vị trí dựa trên chỉ mục
Python3
Misha walked and looked around.5
Misha walked and looked around.6
Joe stood up and spoke to the crowd. There are 4 dogs.18
Joe stood up and spoke to the crowd. There are 4 dogs.11
Joe stood up and spoke to the crowd. There are 4 dogs.12
Misha walked and looked around.6
Joe stood up and spoke to the crowd. There are 4 dogs.42
Joe stood up and spoke to the crowd. There are 4 dogs.43
Joe stood up and spoke to the crowd. There are 4 dogs.44______845____843
Joe stood up and spoke to the crowd. There are 4 dogs.47
Misha walked and looked around.92
đầu ra
Joe stood up and spoke to the crowd. There are 4 dogs.0
- Chúng tôi có thể chèn các đối tượng bằng cách sử dụng các từ khóa được chỉ định
Python3
Misha walked and looked around.5
Misha walked and looked around.6
Misha walked and looked around.01
Joe stood up and spoke to the crowd. There are 4 dogs.11
Joe stood up and spoke to the crowd. There are 4 dogs.12
Misha walked and looked around.04
Joe stood up and spoke to the crowd. There are 4 dogs.5
Misha walked and looked around.06
Joe stood up and spoke to the crowd. There are 4 dogs.43
Misha walked and looked around.08
Misha walked and looked around.09
Joe stood up and spoke to the crowd. There are 4 dogs.5
Misha walked and looked around.11
Joe stood up and spoke to the crowd. There are 4 dogs.43
Misha walked and looked around.08
Misha walked and looked around.14
Joe stood up and spoke to the crowd. There are 4 dogs.5
Misha walked and looked around.16
Misha walked and looked around.92
đầu ra
Joe stood up and spoke to the crowd. There are 4 dogs.1
- Chúng ta có thể sử dụng lại các đối tượng đã chèn để tránh trùng lặp
Python3
Misha walked and looked around.5
Misha walked and looked around.6
Misha walked and looked around.20
Joe stood up and spoke to the crowd. There are 4 dogs.11
Joe stood up and spoke to the crowd. There are 4 dogs.12
Misha walked and looked around.23
Joe stood up and spoke to the crowd. There are 4 dogs.5
Misha walked and looked around.25
Misha walked and looked around.92
đầu ra
Giây đầu tiên không sao, nhưng giây thứ hai thật khó khăn.
Độ chính xác nổi với. phương thức định dạng[]
cú pháp. {[mục lục]. [chiều rộng][. độ chính xác][loại]}
Loại có thể được sử dụng với mã định dạng
- 'd' cho số nguyên
- 'f' cho số dấu phẩy động
- 'b' cho số nhị phân
- 'o' cho số bát phân
- 'x' cho số thập lục phân bát phân
- 's' cho chuỗi
- 'e' cho dấu phẩy động ở định dạng số mũ
Ví dụ
Python3
Misha walked and looked around.5
Misha walked and looked around.6
Misha walked and looked around.29
Joe stood up and spoke to the crowd. There are 4 dogs.1
Misha walked and looked around.91
Joe stood up and spoke to the crowd. There are 4 dogs.3
Misha walked and looked around.33
Misha walked and looked around.5
Misha walked and looked around.6
Misha walked and looked around.36
Joe stood up and spoke to the crowd. There are 4 dogs.11
Joe stood up and spoke to the crowd. There are 4 dogs.12
Misha walked and looked around.6______391
Misha walked and looked around.92
đầu ra
Joe stood up and spoke to the crowd. There are 4 dogs.4
Ghi chú. Để biết thêm về str. format[], tham khảo hàm format[] trong Python
Phương pháp 3. Chuỗi được định dạng bằng cách sử dụng chuỗi F
PEP 498 đã giới thiệu một cơ chế định dạng chuỗi mới được gọi là Nội suy chuỗi ký tự hoặc phổ biến hơn là chuỗi F [do ký tự f đứng trước ký tự chuỗi ký tự]. Ý tưởng đằng sau chuỗi f là làm cho phép nội suy chuỗi trở nên đơn giản hơn
Để tạo chuỗi f, thêm chữ cái “ f ” vào trước chuỗi. Bản thân chuỗi có thể được định dạng giống như cách bạn làm với str. định dạng[]. Chuỗi F cung cấp một cách ngắn gọn và thuận tiện để nhúng các biểu thức python bên trong các chuỗi ký tự để định dạng
Ví dụ. Định dạng chuỗi với F-Strings
Python3
Misha walked and looked around.42
Joe stood up and spoke to the crowd. There are 4 dogs.5
Misha walked and looked around.44
Misha walked and looked around.5
Misha walked and looked around.46
Misha walked and looked around.47
Joe stood up and spoke to the crowd. There are 4 dogs.3
đầu ra
Misha walked and looked around.0
Cú pháp định dạng mới này rất mạnh mẽ và dễ dàng. Bạn cũng có thể chèn các biểu thức Python tùy ý và thậm chí bạn có thể thực hiện các phép tính số học trong đó
Ví dụ. Các phép toán số học sử dụng chuỗi F
Python3
Misha walked and looked around.49
Joe stood up and spoke to the crowd. There are 4 dogs.5
Misha walked and looked around.51
Misha walked and looked around.09
Joe stood up and spoke to the crowd. There are 4 dogs.5
Misha walked and looked around.54
_______05
Misha walked and looked around.46____057____53
đầu ra
Misha walked and looked around.1
Chúng ta cũng có thể sử dụng các biểu thức lambda trong định dạng chuỗi f
Ví dụ. Biểu thức Lambda sử dụng chuỗi F
Python3
Misha walked and looked around.5
Misha walked and looked around.46
Misha walked and looked around.61
Joe stood up and spoke to the crowd. There are 4 dogs.3
đầu ra
Misha walked and looked around.2
Độ chính xác nổi trong phương thức f-String
cú pháp. {giá trị. {chiều rộng}. {độ chính xác}}
Ví dụ. Float Precision sử dụng dây F
Python3
Misha walked and looked around.63
Joe stood up and spoke to the crowd. There are 4 dogs.5
Misha walked and looked around.65
Misha walked and looked around.5
Misha walked and looked around.46
Misha walked and looked around.68
Joe stood up and spoke to the crowd. There are 4 dogs.3
đầu ra
Misha walked and looked around.3
Ghi chú. Để biết thêm về chuỗi f, hãy tham khảo Chuỗi f trong Python
Phương pháp 4. Lớp mẫu chuỗi
Trong mô-đun Chuỗi, Lớp mẫu cho phép chúng tôi tạo cú pháp đơn giản hóa cho đặc tả đầu ra. Định dạng sử dụng các tên giữ chỗ được tạo bởi $ với các mã định danh Python hợp lệ [ký tự chữ và số và dấu gạch dưới]. Bao quanh chỗ dành sẵn bằng dấu ngoặc nhọn cho phép nó được theo sau bởi nhiều chữ cái và số hơn mà không có khoảng trắng ở giữa. Viết $$ tạo một $ thoát duy nhất
Ví dụ. Định dạng chuỗi bằng Lớp mẫu
Python3
Misha walked and looked around.70
Misha walked and looked around.71
Misha walked and looked around.72
Joe stood up and spoke to the crowd. There are 4 dogs.03____074
Misha walked and looked around.75
Misha walked and looked around.76
Joe stood up and spoke to the crowd. There are 4 dogs.5
Misha walked and looked around.78
Misha walked and looked around.79
Joe stood up and spoke to the crowd. There are 4 dogs.5
Misha walked and looked around.81
Misha walked and looked around.82
Misha walked and looked around.83
Misha walked and looked around.84
Misha walked and looked around.85
Joe stood up and spoke to the crowd. There are 4 dogs.5
Misha walked and looked around.87
Misha walked and looked around.88
Joe stood up and spoke to the crowd. There are 4 dogs.3
Misha walked and looked around.90
Misha walked and looked around.91
Misha walked and looked around.5
Misha walked and looked around.93____55
Misha walked and looked around.95
Joe stood up and spoke to the crowd. There are 4 dogs.5
Misha walked and looked around.97
đầu ra
Misha walked and looked around.4
Ghi chú. Để biết thêm về lớp Mẫu chuỗi, hãy tham khảo Lớp mẫu chuỗi trong Python
Định dạng chuỗi. % so với. . định dạng so với. f-chuỗi ký tự
chuỗi f nhanh hơn và tốt hơn cả %-formatting và str. định dạng[]. Các biểu thức chuỗi f được đánh giá trong thời gian chạy và chúng ta cũng có thể nhúng các biểu thức bên trong chuỗi f, sử dụng cú pháp rất đơn giản và dễ dàng. Các biểu thức bên trong dấu ngoặc nhọn được đánh giá trong thời gian chạy và sau đó đặt cùng với phần chuỗi của chuỗi f và sau đó chuỗi cuối cùng được trả về