Đầ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 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ã. Để xuất văn bản vào màn hình, bạn sẽ cần dòng này ::Đầu ra văn bản trong Python [chuỗi]
Chức năng in
Name the file hello.py. It only needs one line of code.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:
Lưu ý: các ký tự $ python hello.py
6 tạo một dòng mới the characters
Hello World
$ python hello.py6 create a new line
Hello World
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.py7
Hello World
$ python hello.py8
Hello World
$ python hello.py9
Hello World
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.py7
Hello World
$ python hello.py8
Hello World
print["Hello World\nThis is a message"]3
print["Hello World\nThis is a message"]0
$ python hello.py7
Hello World
$ python hello.py8
Hello World
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 = 30
print[x]
print["Hello World\nThis is a message"]0
$ python hello.py7
Hello World
$ python hello.py8
Hello World
x = 34
print[x]
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.py0
Hello World
Mã ban đầu cho điều này sẽ trông giống như dưới đây như sau: & nbsp;
Python3
x = 36
print[x]
x = 37
print[x]
x = 38
print[x]
print["Hello World\nThis is a message"]9
30
31
32
33
34
$ python hello.py8
Hello World
36
37__
x = 21
y = 3
print["x = {}, y = {}".format[x,y]]
x = 22
y = 3
print["x = {}, y = {}".format[x,y]]
x = 23
y = 3
print["x = {}, y = {}".format[x,y]]
x = 24
y = 3
print["x = {}, y = {}".format[x,y]]
x = 25
y = 3
print["x = {}, y = {}".format[x,y]]
x = 26
y = 3
print["x = {}, y = {}".format[x,y]]
$ python hello.py7
Hello World
x = 28
y = 3
print["x = {}, y = {}".format[x,y]]
print["Hello World\nThis is a message"]9
x = 2, y = 30
print["Hello World\nThis is a message"]0
x = 26
y = 3
print["x = {}, y = {}".format[x,y]]
x = 2, y = 33
39
print["Hello World\nThis is a message"]0
x = 21
y = 3
print["x = {}, y = {}".format[x,y]]
x = 2, y = 37
x = 25
y = 3
print["x = {}, y = {}".format[x,y]]
x = 26
y = 3
print["x = {}, y = {}".format[x,y]]
$ python hello.py7
Hello World
$ python hello.py8
Hello World
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:
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 = 36
print[x]
x = 37
print[x]
x = 38
print[x]
print["Hello World\nThis is a message"]9
30
31
32
33
34
$ python hello.py8
Hello World
36
37__
x = 21
y = 3
print["x = {}, y = {}".format[x,y]]
x = 22
y = 3
print["x = {}, y = {}".format[x,y]]
x = 23
y = 3
print["x = {}, y = {}".format[x,y]]
x = 24
y = 3
print["x = {}, y = {}".format[x,y]]
x = 25
y = 3
print["x = {}, y = {}".format[x,y]]
x = 26
y = 3
print["x = {}, y = {}".format[x,y]]
$ python hello.py7
Hello World
$ python hello.py8
Hello World
print[value[s], sep= ' ', end = '\n', file=file, flush=flush]2
print["Hello World\nThis is a message"]0
x = 26
y = 3
print["x = {}, y = {}".format[x,y]]
x = 2, y = 33
39
print["Hello World\nThis is a message"]0
x = 21
y = 3
print["x = {}, y = {}".format[x,y]]
x = 2, y = 37
x = 25
y = 3
print["x = {}, y = {}".format[x,y]]
x = 26
y = 3
print["x = {}, y = {}".format[x,y]]
$ python hello.py7
Hello World
$ python hello.py8
Hello World
print[value[s], sep= ' ', end = '\n', file=file, flush=flush]2
print["Hello World\nThis is a message"]0
Output:
//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.py15
Hello World
print["Hello World\nThis is a message"]9
$ python hello.py17
Hello World
$ python hello.py18
Hello World
print["Hello World\nThis is a message"]9
$ python hello.py17
Hello World
$ python hello.py21
Hello World
print["Hello World\nThis is a message"]9
$ python hello.py23
Hello World
$ python hello.py7
Hello World
$ python hello.py25
Hello World
print["Hello World\nThis is a message"]9
$ python hello.py27
Hello World
print["Hello World\nThis is a message"]0
Output:
$ python hello.py1
Hello World
Example:
Máy tách biệt10, 20 and 30 are positional argument where as sep=’ – ‘ is keyword argument.
Python3
$ python hello.py7
Hello World
$ python hello.py8
Hello World
$ python hello.py31
Hello World
$ python hello.py32
Hello World
$ python hello.py33
Hello World
$ python hello.py34
Hello World
print["Hello World\nThis is a message"]9
$ python hello.py36
Hello World
$ python hello.py32
Hello World
$ python hello.py38
Hello World
print["Hello World\nThis is a message"]0
Output:
$ python hello.py2
Hello World
đố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 = 36
print[x]
$ python hello.py41
Hello World
$ python hello.py42
Hello World
print["Hello World\nThis is a message"]9
$ python hello.py44
Hello World
$ python hello.py7
Hello World
$ python hello.py8
Hello World
$ python hello.py47
Hello World
$ python hello.py32
Hello World
$ python hello.py49
Hello World
print["Hello World\nThis is a message"]9
$ python hello.py51
Hello World
$ python hello.py52
Hello World
Output:
$ python hello.py3
Hello World
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.py7
Hello World
$ python hello.py8
Hello World
$ python hello.py55
Hello World
$ python hello.py32
Hello World
$ python hello.py49
Hello World
print["Hello World\nThis is a message"]9
$ python hello.py59
Hello World
$ python hello.py8
Hello World
$ python hello.py61
Hello World
$ python hello.py32
Hello World
$ python hello.py63
Hello World
$ python hello.py64
Hello World
Output:
$ python hello.py4
Hello World
Ví dụ: Sử dụng hàm in [] trong Python Using print[] function in Python
Python3
$ python hello.py7
Hello World
$ python hello.py8
Hello World
$ python hello.py67
Hello World
print["Hello World\nThis is a message"]0
$ python hello.py69
Hello World
print["Hello World\nThis is a message"]9
$ python hello.py71
Hello World
$ python hello.py7
Hello World
$ python hello.py8
Hello World
$ python hello.py74
Hello World
$ python hello.py75
Hello World
$ python hello.py7
Hello World
$ python hello.py8
Hello World
$ python hello.py78
Hello World
$ python hello.py32
Hello World
$ python hello.py80
Hello World
$ python hello.py32
Hello World
$ python hello.py78
Hello World
$ python hello.py34
Hello World
print["Hello World\nThis is a message"]9
$ python hello.py85
Hello World
$ python hello.py7
Hello World
$ python hello.py8
Hello World
$ python hello.py88
Hello World
print["Hello World\nThis is a message"]8
print["Hello World\nThis is a message"]9
$ python hello.py91
Hello World
print["Hello World\nThis is a message"]0
$ python hello.py7
Hello World
$ python hello.py8
Hello World
$ python hello.py95
Hello World
print["Hello World\nThis is a message"]0
Output:
$ python hello.py5
Hello World