Văn bản bao bọc trong bảng Python

Trong Python, để bọc hoặc cắt bớt một chuỗi ở độ rộng nhất định [= số ký tự], hãy sử dụng mô-đun textwrap của thư viện chuẩn

  • textwrap — Gói và điền văn bản — Python 3. 9. 7 tài liệu

Bài viết này mô tả các nội dung sau

  • Quấn một chuỗi.
    print['\n'.join[s_wrap_list]]
    # Python can be easy to pick up whether
    # you're a first time programmer or you're
    # experienced with other languages
    
    4,
    print['\n'.join[s_wrap_list]]
    # Python can be easy to pick up whether
    # you're a first time programmer or you're
    # experienced with other languages
    
    5
  • Cắt ngắn một chuỗi.
    print['\n'.join[s_wrap_list]]
    # Python can be easy to pick up whether
    # you're a first time programmer or you're
    # experienced with other languages
    
    0
  • print['\n'.join[s_wrap_list]]
    # Python can be easy to pick up whether
    # you're a first time programmer or you're
    # experienced with other languages
    
    1 đối tượng

Nếu bạn muốn viết các chuỗi dài trên nhiều dòng trong mã thay vì ở đầu ra, hãy xem bài viết sau

  • Viết một chuỗi dài trên nhiều dòng trong Python

Mô-đun pprint hữu ích cho việc định dạng và xuất danh sách và từ điển. Xem bài viết sau

  • In đẹp với pprint trong Python

Liên kết được tài trợ

Quấn một chuỗi.
print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
4,
print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
5

Với chức năng

print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
4 của mô-đun textwrap, bạn có thể lấy một danh sách được chia theo dấu ngắt từ để khớp với một số ký tự nhất định

Chỉ định số ký tự cho đối số thứ hai

print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
5. Mặc định là
print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
6

import textwrap

s = "Python can be easy to pick up whether you're a first time programmer or you're experienced with other languages"

s_wrap_list = textwrap.wrap[s, 40]
print[s_wrap_list]
# ['Python can be easy to pick up whether', "you're a first time programmer or you're", 'experienced with other languages']

nguồn.

Sử dụng

print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
7 để lấy chuỗi có mã nguồn cấp dữ liệu
print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
8

print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages

nguồn.

Hàm

print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
5 trả về một chuỗi có ngắt dòng, không phải danh sách. Nó giống như
print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
7 sau
print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
4 như trong ví dụ trên

Điều này thuận tiện khi bạn không cần danh sách và muốn xuất một chuỗi có độ rộng cố định

print[textwrap.fill[s, 40]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages

nguồn.

Nếu đối số

print[textwrap.fill[s, 40]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
2 được chỉ định, các dòng sau sẽ bị bỏ qua

print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
1

nguồn.

Nếu bỏ qua, theo mặc định,

print[textwrap.fill[s, 40]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
3 được xuất ở cuối. Nó có thể được thay thế bằng bất kỳ chuỗi nào có đối số
print[textwrap.fill[s, 40]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
4

print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
4

nguồn.

Đối số

print[textwrap.fill[s, 40]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
5 có thể được sử dụng để chỉ định một chuỗi sẽ được thêm vào đầu dòng đầu tiên. Nó được sử dụng khi bạn muốn thụt vào đầu đoạn văn

print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
6

nguồn.

Để biết các cài đặt chi tiết khác, vui lòng tham khảo tài liệu chính thức

  • textwrap — Gói và điền văn bản — Python 3. 9. 7 tài liệu

Cắt ngắn một chuỗi.
print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
0

Nếu bạn muốn rút ngắn một chuỗi bằng cách cắt bớt nó, hãy sử dụng hàm

print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
0 của mô-đun textwrap

Nó có thể được cắt bớt từng từ để phù hợp với một số ký tự nhất định với trình giữ chỗ. Trình giữ chỗ là

print[textwrap.fill[s, 40]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
3, theo mặc định và có thể được đặt bằng đối số
print[textwrap.fill[s, 40]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
4

print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
1

nguồn.

Liên kết được tài trợ

print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
1 đối tượng

Nếu bạn thực hiện

print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
4 hoặc
print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
5 nhiều lần với cùng cài đặt, thì việc tạo đối tượng
print['\n'.join[s_wrap_list]]
# Python can be easy to pick up whether
# you're a first time programmer or you're
# experienced with other languages
1 sẽ hiệu quả hơn

Chủ Đề