Hướng dẫn how do you show text in python? - làm thế nào để bạn hiển thị văn bản trong python?

Đầu ra văn bản trong Python (chuỗi)

Đầu ra văn bản là một trong những điều cơ bản trong lập trình Python. Không phải tất cả các chương trình đều có giao diện người dùng đồ họa, màn hình văn bản thường đủ.

Bạn có thể xuất vào thiết bị đầu cuối với chức năng in. Chức năng này hiển thị văn bản trên màn hình của bạn, nó đã giành được in.

Khóa học liên quan: Khóa học & Bài tập lập trình Python hoàn chỉnh Complete Python Programming Course & Exercises

Chức năng in

Thiết bị đầu cuối là một giao diện rất đơn giản cho các chương trình Python. Mặc dù không sáng bóng như một ứng dụng GUI hoặc Web, nhưng nó đủ tốt để bao gồm những điều cơ bản.

Tạo một chương trình mới (tệp văn bản) trong trình soạn thảo IDE hoặc mã của bạn. Đóng tên tệp hello.py. Nó chỉ cần một dòng mã.
Name the file hello.py. It only needs one line of code.

Để xuất văn bản vào màn hình, bạn sẽ cần dòng này ::

print("Hello World")

Chạy chương trình (từ Terminal: Python Hello.py) Nếu bạn chạy chương trình:
If you run the program:

$ python hello.py
Hello World

In Newline

Chương trình trên in mọi thứ trên một dòng. Tại một số điểm bạn sẽ muốn viết nhiều dòng.

Để viết nhiều dòng, hãy thêm ký tự ‘\ n,:

print("Hello World\nThis is a message")

Kết quả trong:

Hướng dẫn how do you show text in python? - làm thế nào để bạn hiển thị văn bản trong python?

Lưu ý: các ký tự

x = 2
y = 3
print("x = {}, y = {}".format(x,y))
6 tạo một dòng mới the characters
x = 2
y = 3
print("x = {}, y = {}".format(x,y))
6 create a new line

Tải xuống bài tập

In biến

Để in các biến:

x = 3
print(x)

Điều này sẽ hiển thị:

3

Để in nhiều biến trên một dòng:

x = 2
y = 3
print("x = {}, y = {}".format(x,y))

Sẽ cho bạn:

x = 2, y = 3

Nếu bạn là người mới bắt đầu Python, thì tôi đánh giá cao cuốn sách này.

Tải xuống bài tập


Bạn có thể dễ dàng làm điều này với Tkinter, được bao gồm trong hầu hết các cài đặt Python hiện đại. Ví dụ,

import tkinter as tk
from random import seed, choice
from string import ascii_letters

seed(42)

colors = ('red', 'yellow', 'green', 'cyan', 'blue', 'magenta')
def do_stuff():
    s = ''.join([choice(ascii_letters) for i in range(10)])
    color = choice(colors)
    l.config(text=s, fg=color)
    root.after(100, do_stuff)

root = tk.Tk()
root.wm_overrideredirect(True)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
root.bind("", lambda evt: root.destroy())

l = tk.Label(text='', font=("Helvetica", 60))
l.pack(expand=True)

do_stuff()
root.mainloop()

Nhấp vào nút chuột trái để thoát.

Đây chỉ là một bằng chứng của khái niệm. Để kiểm soát màu sắc &/hoặc phông chữ trên cơ sở từng chữ cái, bạn sẽ cần phải làm điều gì đó phức tạp hơn một chút. Bạn có thể sử dụng một hàng các tiện ích nhãn (một cho mỗi chữ cái) hoặc bạn có thể sử dụng tiện ích văn bản.


Tuy nhiên, nếu bạn thử điều này trên máy Mac, cửa sổ có thể không nhận được tiêu điểm, như đã đề cập ở đây. Các câu trả lời ở đây cho thấy một cách khác để có được một cửa sổ toàn màn hình, nhưng tôi nghi ngờ nó có thể bị lỗi tương tự.

root.attributes("-fullscreen", True)

Một lợi thế của phương pháp này là nó không yêu cầu cuộc gọi

x = 2
y = 3
print("x = {}, y = {}".format(x,y))
7.

47 Ví dụ về mã Python được tìm thấy liên quan đến "văn bản hiển thị". Bạn có thể bỏ phiếu cho những người bạn thích hoặc bỏ phiếu cho những người bạn không thích và đi đến dự án gốc hoặc tệp nguồn bằng cách theo các liên kết trên mỗi ví dụ. display text". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

ví dụ 1

def display_text(self, title, text, parent=None, modal=False):
        dialog = InfoDialog(title, None, parent, modal)
        text_view = gtk.TextView()
        text_view.get_buffer().set_text(text)
        text_view.set_editable(False)
        text_view.set_cursor_visible(False)
        text_view.show()
        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        sw.show()
        sw.add(text_view)
        detail_view = gtk.Frame()
        detail_view.set_shadow_type(gtk.SHADOW_IN)
        detail_view.add(sw)
        detail_view.set_border_width(6)
        dialog.vbox.add(detail_view)
        detail_view.show()
        text_view.set_size_request(485, 300)
        dialog.run_async()
        return 

Ví dụ 2

$ python hello.py
Hello World
0

Ví dụ 3

$ python hello.py
Hello World
1

Ví dụ 4

$ python hello.py
Hello World
2

Ví dụ 5

$ python hello.py
Hello World
3

Ví dụ 6

$ python hello.py
Hello World
4

Ví dụ 7

$ python hello.py
Hello World
5

Ví dụ 8

$ python hello.py
Hello World
6

Ví dụ 9

$ python hello.py
Hello World
7

Ví dụ 10

$ python hello.py
Hello World
8

Ví dụ 11

$ python hello.py
Hello World
9

Ví dụ 12

print("Hello World\nThis is a message")
0

Ví dụ 13

print("Hello World\nThis is a message")
1

Ví dụ 14

print("Hello World\nThis is a message")
2

Ví dụ 15

print("Hello World\nThis is a message")
3

Ví dụ 16

$ python hello.py
Hello World
8

Ví dụ 17

print("Hello World\nThis is a message")
5

Ví dụ 18

print("Hello World\nThis is a message")
6

Ví dụ 19

print("Hello World\nThis is a message")
7

Ví dụ 20

print("Hello World\nThis is a message")
8

Ví dụ 21

print("Hello World\nThis is a message")
9

Ví dụ 22

x = 3
print(x)
0

Ví dụ 23

x = 3
print(x)
1

Ví dụ 24

x = 3
print(x)
2

Ví dụ 25

x = 3
print(x)
3

Ví dụ 26

print("Hello World\nThis is a message")
5

Ví dụ 27

print("Hello World\nThis is a message")
5

Ví dụ 28

x = 3
print(x)
6

Ví dụ 29

x = 3
print(x)
7

Ví dụ 30

x = 3
print(x)
8

Ví dụ 31

x = 3
print(x)
9

Ví dụ 32

3
0

Ví dụ 33

3
1

Ví dụ 34

3
2

Ví dụ 35

3
3

Ví dụ 36

3
4

Ví dụ 37

3
5

Ví dụ 38

3
6

Ví dụ 39

3
7

Ví dụ 40

3
8

Ví dụ 41

3
9

Ví dụ 42

x = 2
y = 3
print("x = {}, y = {}".format(x,y))
0

Ví dụ 43

x = 2
y = 3
print("x = {}, y = {}".format(x,y))
1

Ví dụ 44

x = 2
y = 3
print("x = {}, y = {}".format(x,y))
2

Ví dụ 45

print("Hello World\nThis is a message")
5

Ví dụ 46

x = 2
y = 3
print("x = {}, y = {}".format(x,y))
4

Ví dụ 47

x = 2
y = 3
print("x = {}, y = {}".format(x,y))
5

Làm thế nào bạn sẽ in một chuỗi văn bản trong Python?

Để in bất cứ thứ gì trong Python, bạn sử dụng hàm in () - đó là từ khóa in theo sau là một tập hợp dấu ngoặc đơn mở và đóng, ().use the print() function – that is the print keyword followed by a set of opening and closing parentheses, () .

Python sử dụng lệnh nào để hiển thị tin nhắn?

Sử dụng sự phân tán.Các lệnh trong Python ™ để hiển thị các biểu mẫu và tin nhắn tùy chỉnh cho người dùng.disp. commands in Python™ to display custom forms and messages to the user.