Mặc dù không cần thiết phải truyền đối số trong hàm print[], nhưng nó yêu cầu một dấu ngoặc đơn trống ở cuối để yêu cầu python thực thi hàm thay vì gọi nó bằng 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 print[]
Chuỗi ký tự
Chuỗi ký tự trong câu lệnh in của 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 print[]
- \N. Chuỗi ký tự này được sử dụng để thêm một dòng trống mới trong khi in một câu lệnh
- “”. Một trích dẫn trống [“”] được sử dụng để in một dòng trống
Thí dụ
Python3
print
GeeksforGeeks is best for DSA Content.0_______0_______1
GeeksforGeeks is best for DSA Content.2
đầu ra
GeeksforGeeks is best for DSA Content.
kết thúc = ” ” tuyên bố
Từ khóa end được sử dụng để chỉ định nội dung sẽ được in khi kết thúc thực thi hàm print[]. Theo mặc định, nó được đặt thành “\n”, dẫn đến thay đổi dòng sau khi thực thi câu lệnh print[]
Thí dụ. Python print[] không có dòng mới
Python3
GeeksforGeeks is best for DSA Content.3
GeeksforGeeks is best for DSA Content.4
print
GeeksforGeeks is best for DSA Content.0_______0_______7
GeeksforGeeks is best for DSA Content.2
GeeksforGeeks is best for DSA Content.9
print
GeeksforGeeks is best for DSA Content.0
GeeksforGeeks is best for DSA Content.7
GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG3
GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG4
GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG5
GeeksforGeeks is best for DSA Content.2
print
GeeksforGeeks is best for DSA Content.0_______12_______9
GeeksforGeeks is best for DSA Content.2
đầu ra
GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG
đối số tuôn ra
I/O trong python thường được đệm, nghĩa là chúng được sử dụng theo khối. Đây là lúc chức năng tuôn ra xuất hiện vì nó giúp người dùng quyết định xem họ có cần đệm nội dung bằng văn bản hay không. Theo mặc định, nó được đặt thành false. Nếu nó được đặt thành true, đầu ra sẽ được viết dưới dạng một chuỗi ký tự nối tiếp nhau. Quá trình này diễn ra chậm đơn giản vì viết từng đoạn dễ hơn là viết từng ký tự một. Để hiểu trường hợp sử dụng đối số tuôn ra trong hàm print[], hãy lấy một ví dụ
Thí dụ
Hãy tưởng tượng bạn đang xây dựng đồng hồ đếm ngược, đồng hồ này sẽ thêm thời gian còn lại vào cùng một dòng mỗi giây. Nó sẽ trông giống như dưới đây
3>>>2>>>1>>>Start
Mã ban đầu cho điều này sẽ trông giống như bên dưới như sau.
Python3
3>>>2>>>1>>>Start1
3>>>2>>>1>>>Start2
3>>>2>>>1>>>Start3
GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG4
3>>>2>>>1>>>Start5
3>>>2>>>1>>>Start6
3>>>2>>>1>>>Start7
3>>>2>>>1>>>Start8
3>>>2>>>1>>>Start9
GeeksforGeeks is best for DSA Content.0
12-12-20221
12-12-20222
12-12-20223
12-12-20224
12-12-20225
12-12-20226_______31_______7
12-12-20228
12-12-20229_______40_______0
File "0b97e8c5-bacf-4e89-9ea3-c5510b916cdb.py", line 1 print[10, 20, sep=' - ', 30] ^ SyntaxError: positional argument follows keyword argument1
print
40_______3GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG4
File "0b97e8c5-bacf-4e89-9ea3-c5510b916cdb.py", line 1 print[10, 20, sep=' - ', 30] ^ SyntaxError: positional argument follows keyword argument5
GeeksforGeeks is best for DSA Content.2
File "0b97e8c5-bacf-4e89-9ea3-c5510b916cdb.py", line 1 print[10, 20, sep=' - ', 30] ^ SyntaxError: positional argument follows keyword argument1
File "0b97e8c5-bacf-4e89-9ea3-c5510b916cdb.py", line 1 print[10, 20, sep=' - ', 30] ^ SyntaxError: positional argument follows keyword argument8
12-12-20224
GeeksforGeeks is best for DSA Content.2
12-12-20226_______51_______2
File "0b97e8c5-bacf-4e89-9ea3-c5510b916cdb.py", line 1 print[10, 20, sep=' - ', 30] ^ SyntaxError: positional argument follows keyword argument0
File "0b97e8c5-bacf-4e89-9ea3-c5510b916cdb.py", line 1 print[10, 20, sep=' - ', 30] ^ SyntaxError: positional argument follows keyword argument1
print
___GeeksforGeeks is best for DSA Content.0
'Hello Geeks!!\n'7
GeeksforGeeks is best for DSA Content.2
Vì vậy, đoạn mã trên thêm văn bản mà không có dòng mới ở cuối và sau đó ngủ trong một giây sau mỗi lần thêm văn bản. Khi kết thúc đếm ngược, nó in Bắt đầu và kết thúc dòng. Nếu bạn chạy mã như hiện tại, nó sẽ đợi trong 3 giây và đột ngột in toàn bộ văn bản cùng một lúc. Điều này gây lãng phí 3 giây do bộ đệm của đoạn văn bản như hình bên dưới
Mặc dù bộ đệm phục vụ một mục đích, nhưng nó có thể dẫn đến các hiệu ứng không mong muốn như được hiển thị ở trên. Để giải quyết vấn đề tương tự, đối số tuôn ra được sử dụng với hàm print[]. Bây giờ, đặt đối số flush là true và xem lại kết quả
Python3
3>>>2>>>1>>>Start1
3>>>2>>>1>>>Start2
3>>>2>>>1>>>Start3
GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG4
3>>>2>>>1>>>Start5
3>>>2>>>1>>>Start6
3>>>2>>>1>>>Start7
3>>>2>>>1>>>Start8
3>>>2>>>1>>>Start9
GeeksforGeeks is best for DSA Content.0
12-12-20221
12-12-20222
12-12-20223
12-12-20224
12-12-20225
12-12-20226_______31_______7
12-12-20228
12-12-20229_______40_______0
File "0b97e8c5-bacf-4e89-9ea3-c5510b916cdb.py", line 1 print[10, 20, sep=' - ', 30] ^ SyntaxError: positional argument follows keyword argument1
print
40_______3GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG4
File "0b97e8c5-bacf-4e89-9ea3-c5510b916cdb.py", line 1 print[10, 20, sep=' - ', 30] ^ SyntaxError: positional argument follows keyword argument5
print
4GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG4
print
6GeeksforGeeks is best for DSA Content.2
File "0b97e8c5-bacf-4e89-9ea3-c5510b916cdb.py", line 1 print[10, 20, sep=' - ', 30] ^ SyntaxError: positional argument follows keyword argument1
File "0b97e8c5-bacf-4e89-9ea3-c5510b916cdb.py", line 1 print[10, 20, sep=' - ', 30] ^ SyntaxError: positional argument follows keyword argument8
12-12-20224
GeeksforGeeks is best for DSA Content.2
12-12-20226_______51_______2
File "0b97e8c5-bacf-4e89-9ea3-c5510b916cdb.py", line 1 print[10, 20, sep=' - ', 30] ^ SyntaxError: positional argument follows keyword argument0
File "0b97e8c5-bacf-4e89-9ea3-c5510b916cdb.py", line 1 print[10, 20, sep=' - ', 30] ^ SyntaxError: positional argument follows keyword argument1
print
___GeeksforGeeks is best for DSA Content.0
'Hello Geeks!!\n'7
GeeksforGeeks is best for DSA Content.2
đầu ra
https. //phương tiện truyền thông. chuyên viên máy tính. org/wp-content/uploads/20201222163647/Untitled26---Jupyter-Notebook---Google-Chrome-2020-12-22-16-33-02. mp4
dấu phân cách
Hàm print[] có thể chấp nhận bất kỳ số lượng đối số vị trí nào. Để tách các đối số vị trí này, đối số từ khóa “sep” được sử dụng
Ghi chú. Vì sep , end , flush , file là các đối số từ khóa nên vị trí của chúng không thay đổi kết quả của mã.
Thí dụ
Python3
GeeksforGeeks is best for DSA Content.10_______12_______4
GeeksforGeeks is best for DSA Content.12
GeeksforGeeks is best for DSA Content.13
GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG4
GeeksforGeeks is best for DSA Content.12
GeeksforGeeks is best for DSA Content.16_______12_______4
GeeksforGeeks is best for DSA Content.18
print
GeeksforGeeks is best for DSA Content.20
GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG4
GeeksforGeeks is best for DSA Content.22
GeeksforGeeks is best for DSA Content.2
đầu ra
12-12-2022
Thí dụ
Đối số vị trí không thể xuất hiện sau đối số từ khóa. Trong ví dụ dưới đây 10, 20 và 30 là đối số vị trí trong đó as sep=’ – ‘ là đối số từ khóa
Python3
print
0_______0
GeeksforGeeks is best for DSA Content.26_______0_______27
GeeksforGeeks is best for DSA Content.28
GeeksforGeeks is best for DSA Content.29
GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG4
GeeksforGeeks is best for DSA Content.31
GeeksforGeeks is best for DSA Content.27
GeeksforGeeks is best for DSA Content.33
GeeksforGeeks is best for DSA Content.2
đầu ra
File "0b97e8c5-bacf-4e89-9ea3-c5510b916cdb.py", line 1 print[10, 20, sep=' - ', 30] ^ SyntaxError: positional argument follows keyword argument
đối số tập tin
Trái với suy nghĩ của nhiều người, hàm print[] không chuyển đổi 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] theo byte. Hàm print[] là một giao diện trên các lớp này, ủy quyền việc in thực tế cho một đối tượng giống như luồng hoặc tệp. Theo mặc định, chức năng print[] được liên kết với sys. thiết bị xuất chuẩn thông qua đối số tệp.
Thí dụ. In Python[] vào tệp
Python3
3>>>2>>>1>>>Start1
GeeksforGeeks is best for DSA Content.36
GeeksforGeeks is best for DSA Content.37
GeeksforGeeks is best for DSA Content.38
GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG4
GeeksforGeeks is best for DSA Content.40
GeeksforGeeks is best for DSA Content.41
print
0_______0
GeeksforGeeks is best for DSA Content.44_______0_______27_______0_______46
GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG4
GeeksforGeeks is best for DSA Content.48
GeeksforGeeks is best for DSA Content.49
GeeksforGeeks is best for DSA Content.50
đầu ra
'Hello Geeks!!\n'
Thí dụ. với chức năng print[] để ghi nội dung trực tiếp vào tệp văn bản
Python3
print
0_______0
GeeksforGeeks is best for DSA Content.53_______0_______27_______0_______46
GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG4
GeeksforGeeks is best for DSA Content.57
GeeksforGeeks is best for DSA Content.0_______0_______59
GeeksforGeeks is best for DSA Content.27
GeeksforGeeks is best for DSA Content.61
GeeksforGeeks is best for DSA Content.62
đầu ra
% nano Testfile.txt ----------------------------------------------------------------------------------------------- UW PICO 5.09 File: Testfile.txt Welcome to GeeksforGeeks Python world.!! ^G Get Help ^O WriteOut ^R Read File ^Y Prev Pg ^K Cut Text ^C Cur Pos ^X Exit ^J Justify ^W Where is ^V Next Pg ^U UnCut Text ^T To Spell
Thí dụ. Sử dụng hàm print[] trong Python
Python3
GeeksforGeeks is best for DSA Content.63
GeeksforGeeks is best for DSA Content.64
GeeksforGeeks is best for DSA Content.65
GeeksforGeeks is best for DSA Content.66
print
GeeksforGeeks is best for DSA Content.0_______0_______69
GeeksforGeeks is best for DSA Content.2
GeeksforGeeks is best for DSA Content.71_______12_______4
GeeksforGeeks is best for DSA Content.73
GeeksforGeeks is best for DSA Content.74
print
GeeksforGeeks is best for DSA Content.0_______0_______77
GeeksforGeeks is best for DSA Content.78
GeeksforGeeks is best for DSA Content.79
print
0_______0
GeeksforGeeks is best for DSA Content.82
GeeksforGeeks is best for DSA Content.27_______0_______84
GeeksforGeeks is best for DSA Content.27
GeeksforGeeks is best for DSA Content.82
GeeksforGeeks is best for DSA Content.29
GeeksForGeeks is the best platform for DSA content GeeksForGeeks is the best platform for DSA content**Welcome to GFG4
GeeksforGeeks is best for DSA Content.89