Hướng dẫn what python command is used to output text to the screen? - lệnh python nào được sử dụng để xuất văn bản ra màn hình?

Đầ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 what python command is used to output text to the screen? - lệnh python nào được sử dụng để xuất văn bản ra màn hình?

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

$ python hello.py
Hello World
6 tạo một dòng mới the characters
$ python hello.py
Hello World
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


Hàm python print () in thông báo lên màn hình hoặc bất kỳ thiết bị đầu ra tiêu chuẩn nào khác.prints the message to the screen or any other standard output device.

Syntax: 

print(value(s), sep= ' ', end = '\n', file=file, flush=flush)

Parameters:  

  • Giá trị (s): Bất kỳ giá trị nào, và bao nhiêu tùy thích. Sẽ được chuyển đổi thành một chuỗi trước khi inAny value, and as many as you like. Will be converted to a string before printed
  • SEP = Voi phân tách, (tùy chọn) Chỉ định cách tách các đối tượng, nếu có nhiều hơn một.default: Hồi ‘(Optional) Specify how to separate the objects, if there is more than one.Default :’ ‘
  • end = kết thúc(Optional) Specify what to print at the end.Default : ‘\n’
  • Tệp: (Tùy chọn) Một đối tượng có phương thức ghi. Mặc định: sys.stdout(Optional) An object with a write method. Default :sys.stdout
  • Flush: (Tùy chọn) Một boolean, chỉ định nếu đầu ra được xả (đúng) hoặc đệm (sai). Mặc định: Sai(Optional) A Boolean, specifying if the output is flushed (True) or buffered (False). Default: False

Loại trả về: Nó trả lại đầu ra cho màn hình.It returns output to the screen.

Mặc dù không cần thiết phải truyền các đối số trong hàm in (), nhưng nó yêu cầu một dấu ngoặc đơn trống ở cuối để bảo Python thực thi chức năng thay vì gọi nó theo tên. Bây giờ, hãy để khám phá các đối số tùy chọn có thể được sử dụng với hàm in ().

Chuỗi chữ

Chuỗi chữ trong câu lệnh in Python, chủ yếu được sử dụng để định dạng hoặc thiết kế cách một chuỗi cụ thể xuất hiện khi được in bằng hàm in ().

  • \ n: Chuỗi này theo nghĩa đen này được sử dụng để thêm một dòng trống mới trong khi in câu lệnh. This string literal is used to add a new blank line while printing a statement.
  • Một số người khác: Một trích dẫn trống (Hồi) được sử dụng để in một dòng trống. An empty quote (“”) is used to print an empty line.

Example:

Python3

$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
$ python hello.py
Hello World
9
print("Hello World\nThis is a message")
0

Output:

GeeksforGeeks 
 is best for DSA Content.

kết thúc = tuyên bố

Từ khóa kết thúc được sử dụng để chỉ định nội dung sẽ được in ở cuối hàm thực thi in (). Theo mặc định, nó được đặt thành Hồi \ n, dẫn đến việc thay đổi dòng sau khi thực hiện câu lệnh in ().

Ví dụ: python print () không có dòng mớiPython print() without new line

Python3

$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
print("Hello World\nThis is a message")
3
print("Hello World\nThis is a message")
0

$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
print("Hello World\nThis is a message")
3
print("Hello World\nThis is a message")
8
print("Hello World\nThis is a message")
9
x = 3
print(x)
0
print("Hello World\nThis is a message")
0

$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
x = 3
print(x)
4
print("Hello World\nThis is a message")
0

Output:

GeeksForGeeks is the best platform for DSA content
GeeksForGeeks is the best platform for DSA content**Welcome to GFG

Tranh cãi tuôn ra

I/OS trong Python thường được đệm, có nghĩa là chúng được sử dụng trong các khối. Đây là nơi Flush xuất hiện vì nó giúp người dùng quyết định xem họ có cần nội dung bằng văn bản hay không. Theo mặc định, nó được đặt thành sai. Nếu nó được đặt thành True, đầu ra sẽ được viết dưới dạng một chuỗi các ký tự hết lần này đến lần khác. Quá trình này chậm đơn giản vì nó dễ dàng viết bằng các khối hơn là viết một nhân vật tại một thời điểm. Để hiểu trường hợp sử dụng của đối số tuôn ra trong hàm in (), hãy để lấy một ví dụ.

Example:

Hãy tưởng tượng bạn đang xây dựng một bộ đếm thời gian đếm ngược, nối thời gian còn lại cho cùng một dòng mỗi giây. Nó sẽ trông giống như dưới đây:

$ python hello.py
Hello World
0

Mã ban đầu cho điều này sẽ trông giống như dưới đây như sau: & nbsp;

Python3

x = 3
print(x)
6
x = 3
print(x)
7

x = 3
print(x)
8
print("Hello World\nThis is a message")
9
3
0

3
1
3
2
3
3
3
4
$ python hello.py
Hello World
8
3
6
3
7__

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

x = 2
y = 3
print("x = {}, y = {}".format(x,y))
6
$ python hello.py
Hello World
7
x = 2
y = 3
print("x = {}, y = {}".format(x,y))
8
print("Hello World\nThis is a message")
9
x = 2, y = 3
0
print("Hello World\nThis is a message")
0

x = 2
y = 3
print("x = {}, y = {}".format(x,y))
6
x = 2, y = 3
3
3
9
print("Hello World\nThis is a message")
0

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

x = 2
y = 3
print("x = {}, y = {}".format(x,y))
6
$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
print(value(s), sep= ' ', end = '\n', file=file, flush=flush)
2
print("Hello World\nThis is a message")
0

Vì vậy, mã ở trên thêm văn bản mà không có dòng mới và sau đó ngủ trong một giây sau mỗi lần bổ sung văn bản. Khi kết thúc đếm ngược, nó in bắt đầu và chấm dứt dòng. Nếu bạn chạy mã như nó là, nó chờ 3 giây và đột ngột in toàn bộ văn bản cùng một lúc. Đây là một sự lãng phí 3 giây gây ra do bộ đệm của đoạn văn bản như hình dưới đây:

Hướng dẫn what python command is used to output text to the screen? - lệnh python nào được sử dụng để xuất văn bản ra màn hình?

Mặc dù bộ đệm phục vụ một mục đích, nó có thể dẫn đến các hiệu ứng không mong muốn như hình trên. Để chống lại cùng một vấn đề, đối số Flush được sử dụng với hàm in (). Bây giờ, đặt đối số Flush là đúng và một lần nữa xem kết quả.

Python3

x = 3
print(x)
6
x = 3
print(x)
7

x = 3
print(x)
8
print("Hello World\nThis is a message")
9
3
0

3
1
3
2
3
3
3
4
$ python hello.py
Hello World
8
3
6
3
7__

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

x = 2
y = 3
print("x = {}, y = {}".format(x,y))
6
$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
print(value(s), sep= ' ', end = '\n', file=file, flush=flush)
2
print("Hello World\nThis is a message")
0

x = 2
y = 3
print("x = {}, y = {}".format(x,y))
6
x = 2, y = 3
3
3
9
print("Hello World\nThis is a message")
0

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

x = 2
y = 3
print("x = {}, y = {}".format(x,y))
6
$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
print(value(s), sep= ' ', end = '\n', file=file, flush=flush)
2
print("Hello World\nThis is a message")
0

Output:

https://media.geeksforgeeks.org/wp-content/uploads/20201222163647/Untitled26---Jupyter-Notebook---Google-Chrome-2020-12-22-16-33-02.mp4

Vì vậy, mã ở trên thêm văn bản mà không có dòng mới và sau đó ngủ trong một giây sau mỗi lần bổ sung văn bản. Khi kết thúc đếm ngược, nó in bắt đầu và chấm dứt dòng. Nếu bạn chạy mã như nó là, nó chờ 3 giây và đột ngột in toàn bộ văn bản cùng một lúc. Đây là một sự lãng phí 3 giây gây ra do bộ đệm của đoạn văn bản như hình dưới đây:

Mặc dù bộ đệm phục vụ một mục đích, nó có thể dẫn đến các hiệu ứng không mong muốn như hình trên. Để chống lại cùng một vấn đề, đối số Flush được sử dụng với hàm in (). Bây giờ, đặt đối số Flush là đúng và một lần nữa xem kết quả.

Các

Example:

Python3

$ python hello.py
Hello World
15
print("Hello World\nThis is a message")
9
$ python hello.py
Hello World
17

$ python hello.py
Hello World
18
print("Hello World\nThis is a message")
9
$ python hello.py
Hello World
17

$ python hello.py
Hello World
21
print("Hello World\nThis is a message")
9
$ python hello.py
Hello World
23

$ python hello.py
Hello World
7
$ python hello.py
Hello World
25
print("Hello World\nThis is a message")
9
$ python hello.py
Hello World
27
print("Hello World\nThis is a message")
0

Output:

$ python hello.py
Hello World
1

Example:

Máy tách biệt10, 20 and 30 are positional argument where as sep=’ – ‘ is keyword argument.

Python3

$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
$ python hello.py
Hello World
31
$ python hello.py
Hello World
32
$ python hello.py
Hello World
33
$ python hello.py
Hello World
34
print("Hello World\nThis is a message")
9
$ python hello.py
Hello World
36
$ python hello.py
Hello World
32
$ python hello.py
Hello World
38
print("Hello World\nThis is a message")
0

Output:

$ python hello.py
Hello World
2

đối số tập tin

Trái với niềm tin phổ biến, hàm in () không chuyển đổi các tin nhắn thành văn bản trên màn hình. Chúng được thực hiện bởi các lớp mã cấp thấp hơn, có thể đọc dữ liệu (tin nhắn) bằng byte. Hàm print () là một giao diện trên các lớp này, giao cho việc in thực tế vào một luồng hoặc đối tượng giống như tệp. Theo mặc định, hàm in () được liên kết với sys.stdout thông qua đối số tệp. & Nbsp;file-like object. By default, the print() function is bound to sys.stdout through the file argument. 

Ví dụ: python print () vào tệp

Python3

x = 3
print(x)
6
$ python hello.py
Hello World
41

$ python hello.py
Hello World
42
print("Hello World\nThis is a message")
9
$ python hello.py
Hello World
44

$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
$ python hello.py
Hello World
47
$ python hello.py
Hello World
32
$ python hello.py
Hello World
49
print("Hello World\nThis is a message")
9
$ python hello.py
Hello World
51

$ python hello.py
Hello World
52

Output:

$ python hello.py
Hello World
3

Ví dụ: với hàm in () để ghi nội dung trực tiếp vào tệp văn bản.

Python3

$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
$ python hello.py
Hello World
55
$ python hello.py
Hello World
32
$ python hello.py
Hello World
49
print("Hello World\nThis is a message")
9
$ python hello.py
Hello World
59
$ python hello.py
Hello World
8
$ python hello.py
Hello World
61
$ python hello.py
Hello World
32
$ python hello.py
Hello World
63
$ python hello.py
Hello World
64

Output:

$ python hello.py
Hello World
4

Ví dụ: Sử dụng hàm in () trong Python Using print() function in Python

Python3

$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
$ python hello.py
Hello World
67
print("Hello World\nThis is a message")
0

$ python hello.py
Hello World
69
print("Hello World\nThis is a message")
9
$ python hello.py
Hello World
71

$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
$ python hello.py
Hello World
74
$ python hello.py
Hello World
75

$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
$ python hello.py
Hello World
78
$ python hello.py
Hello World
32
$ python hello.py
Hello World
80
$ python hello.py
Hello World
32
$ python hello.py
Hello World
78
$ python hello.py
Hello World
34
print("Hello World\nThis is a message")
9
$ python hello.py
Hello World
85

$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
$ python hello.py
Hello World
88
print("Hello World\nThis is a message")
8
print("Hello World\nThis is a message")
9
$ python hello.py
Hello World
91
print("Hello World\nThis is a message")
0

$ python hello.py
Hello World
7
$ python hello.py
Hello World
8
$ python hello.py
Hello World
95
print("Hello World\nThis is a message")
0

Output:

$ python hello.py
Hello World
5

Làm thế nào để bạn xuất ra màn hình trong Python?

Python print () hàm hàm in () in thông báo được chỉ định vào màn hình hoặc thiết bị đầu ra tiêu chuẩn khác. Thông báo có thể là một chuỗi hoặc bất kỳ đối tượng nào khác, đối tượng sẽ được chuyển đổi thành một chuỗi trước khi được ghi vào màn hình.print() Function The print() function prints the specified message to the screen, or other standard output device. The message can be a string, or any other object, the object will be converted into a string before written to the screen.

Làm thế nào để bạn hiển thị văn bản trên màn hình trong Python?

Trong Python, câu lệnh in được sử dụng để hiển thị văn bản. Trong Python với câu lệnh in, bạn có thể sử dụng trích dẫn đơn (') hoặc trích dẫn kép (").print statement is used to display text. In python with the print statement, you can use Single Quotes(') or Double Quotes(").

Lệnh nào tạo ra đầu ra văn bản trong Python?

In ấn rất có thể là điều đầu tiên bạn sẽ học được khi bắt đầu hành trình học tập Python của mình.Đó là một phần của một truyền thống để viết một chương trình "Hello World" như các dòng mã đầu tiên của bạn.Và bạn làm điều đó bằng cách sử dụng hàm in () để xuất đoạn văn bản đó vào bảng điều khiển.print() function to output that piece of text to the console.

Làm thế nào để bạn in đầu ra văn bản trong Python?

Hàm python print () có bất kỳ số lượng tham số nào và in chúng ra trên một dòng văn bản.Các mục được chuyển đổi thành mẫu văn bản, được phân tách bằng khoảng trắng và có một '\ n' ở cuối (char "newline").Khi được gọi với các tham số bằng không, print () chỉ in '\ n' và không có gì khác.. The items are each converted to text form, separated by spaces, and there is a single '\n' at the end (the "newline" char). When called with zero parameters, print() just prints the '\n' and nothing else.