Hướng dẫn what is text alignment python? - Python căn chỉnh văn bản là gì?

Ghi chú

Nhấn vào đây để tải xuống mã ví dụ đầy đủhere to download the full example code

Các văn bản được căn chỉnh so với điểm neo của chúng tùy thuộc vào các thuộc tính horizontalalignmentverticalalignment.

(Mã nguồn, PNG)

Hướng dẫn what is text alignment python? - Python căn chỉnh văn bản là gì?

Biểu đồ sau đây sử dụng điều này để sắp xếp văn bản so với hình chữ nhật được vẽ.

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

# Build a rectangle in axes coords
left, width = .25, .5
bottom, height = .25, .5
right = left + width
top = bottom + height
p = plt.Rectangle((left, bottom), width, height, fill=False)
p.set_transform(ax.transAxes)
p.set_clip_on(False)
ax.add_patch(p)

ax.text(left, bottom, 'left top',
        horizontalalignment='left',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(left, bottom, 'left bottom',
        horizontalalignment='left',
        verticalalignment='bottom',
        transform=ax.transAxes)

ax.text(right, top, 'right bottom',
        horizontalalignment='right',
        verticalalignment='bottom',
        transform=ax.transAxes)

ax.text(right, top, 'right top',
        horizontalalignment='right',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(right, bottom, 'center top',
        horizontalalignment='center',
        verticalalignment='top',
        transform=ax.transAxes)

ax.text(left, 0.5 * (bottom + top), 'right center',
        horizontalalignment='right',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(left, 0.5 * (bottom + top), 'left center',
        horizontalalignment='left',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(0.5 * (left + right), 0.5 * (bottom + top), 'middle',
        horizontalalignment='center',
        verticalalignment='center',
        transform=ax.transAxes)

ax.text(right, 0.5 * (bottom + top), 'centered',
        horizontalalignment='center',
        verticalalignment='center',
        rotation='vertical',
        transform=ax.transAxes)

ax.text(left, top, 'rotated\nwith newlines',
        horizontalalignment='center',
        verticalalignment='center',
        rotation=45,
        transform=ax.transAxes)

ax.set_axis_off()

plt.show()

Hướng dẫn what is text alignment python? - Python căn chỉnh văn bản là gì?

Phòng trưng bày được tạo ra bởi Sphinx-Gallery

Căn chỉnh văn bản có nghĩa là gì?

Căn chỉnh văn bản là một thuộc tính định dạng đoạn văn xác định sự xuất hiện của văn bản trong cả một đoạn văn. Ví dụ, trong một đoạn được liên kết bên trái (căn chỉnh phổ biến nhất), văn bản được căn chỉnh với lề trái. Trong một đoạn văn hợp lý, văn bản được căn chỉnh với cả hai lề.a paragraph formatting attribute that determines the appearance of the text in a whole paragraph. For example, in a paragraph that is left-aligned (the most common alignment), text is aligned with the left margin. In a paragraph that is justified, text is aligned with both margins.

Căn chỉnh Python là gì?

Thuộc tính căn chỉnh của một đối tượng biến được hoặc đặt căn chỉnh của các giá trị dữ liệu được hiển thị trong Trình chỉnh sửa dữ liệu.Nó không có tác dụng đối với định dạng của các biến hoặc hiển thị các biến hoặc giá trị trong các cửa sổ khác hoặc kết quả được in.. It has no effect on the format of the variables or the display of the variables or values in other windows or printed results.

Tại sao chúng ta sử dụng căn chỉnh văn bản?

Nó cho phép thành phần của một tài liệu văn bản bằng cách sử dụng định vị văn bản khác nhau trên toàn bộ hoặc được chọn của một trang.enables the composition of a text document using different text positioning on the whole or selected part of a page.

Làm thế nào để bạn sắp xếp các câu trong Python?

Bạn có thể sử dụng tùy chọn:>,: use the :> , :< or :^ option in the f-format to left align, right align or center align the text that you want to format. We can use the fortmat() string function in python to output the desired text in the order we want.