Python บรรทัดใหม่: วิธีพิมพ์โดยไม่ต้องขึ้นบรรทัดใหม่ Python
⚡ สรุปอย่างชาญฉลาด
Python โดยค่าเริ่มต้น ฟังก์ชัน print() จะเพิ่มบรรทัดใหม่ แต่คุณสามารถหลีกเลี่ยงได้ ในที่นี้คุณจะได้เรียนรู้เกี่ยวกับพารามิเตอร์ end Python ใช้เครื่องหมายจุลภาค 2 ตัว, sys.stdout.write() และลูปเพื่อให้เอาต์พุตอยู่ในบรรทัดเดียว

Python ฟังก์ชันในตัว print() ใช้เพื่อพิมพ์เนื้อหาที่กำหนดภายในพร้อมท์คำสั่ง ฟังก์ชั่นเริ่มต้นของ Python print คือการเพิ่มอักขระขึ้นบรรทัดใหม่ต่อท้าย
การทำงานของฟังก์ชัน Normal print()
ฟังก์ชัน print() ใช้เพื่อแสดงเนื้อหาใน command prompt หรือ console
นี่คือตัวอย่างที่แสดงให้เห็นการทำงานของ Python พิมพ์โดยไม่มีฟังก์ชันขึ้นบรรทัดใหม่
print("Hello World") print("Welcome to Guru99 Tutorials")
Output:
Hello World Welcome to Guru99 Tutorials
In the given output, you can see that the strings that we have given in print() are displayed on separate lines. The string "Hello World " is printed first, and "Welcome to Guru99 Tutorials" is printed on the next line.
วิธีพิมพ์โดยไม่ต้องขึ้นบรรทัดใหม่ Python?
ราคาเริ่มต้น Python 3+ มีการแนะนำพารามิเตอร์เพิ่มเติมสำหรับ print() ที่เรียกว่า end= พารามิเตอร์นี้ดูแลการลบบรรทัดใหม่ที่เพิ่มโดยค่าเริ่มต้นใน print()
ตัว Vortex Indicator ได้ถูกนำเสนอลงในนิตยสาร Python 3 พิมพ์โดยไม่ต้องขึ้นบรรทัดใหม่ ตัวอย่างด้านล่าง เราต้องการให้สตริงพิมพ์ในบรรทัดเดียวกัน Python. To get that working just add end="" inside print() as shown in the example below:
print("Hello World ", end="") print("Welcome to Guru99 Tutorials")
Output:
Hello World Welcome to Guru99 Tutorials
เราได้ผลลัพธ์ที่ต้องการแล้ว แต่ไม่มีช่องว่างระหว่างข้อความ ข้อความ "Hello World" และ "Welcome to" เหมือนกัน Guruบทเรียนทั้ง 99 บทถูกพิมพ์รวมกันโดยไม่มีช่องว่าง
In order to add space or special character or even string to be printed, the same can be given to the end="" argument, as shown in the example below.
print("Hello World ", end=" ") print("Welcome to Guru99 Tutorials")
So I here we have added one space to the end argument, for example (end=" "). Now, if you see the output, you should see a space between Hello World and Welcome to Guru99 บทเรียน
Output:
Hello World Welcome to Guru99 Tutorials
It's not only space that you can give to the end argument, but you can also specify a string you wish to print between the given strings. So here is an example of it
print("Hello World ", end="It's a nice day! ") print("Welcome to Guru99 Tutorials")
Output:
Hello, World It's a nice day! Welcome to Guru99 Tutorials
พิมพ์โดยไม่ต้องขึ้นบรรทัดใหม่ Python 2.x
หากต้องการให้สตริงพิมพ์โดยไม่ต้องขึ้นบรรทัดใหม่ ใน python2.x คุณจะต้องเพิ่มเครื่องหมายจุลภาค (,) ที่ส่วนท้ายของคำสั่ง print ดังที่แสดงด้านล่าง:
print "Hello World ", print "Welcome to Guru99 Tutorials."
Output:
Hello World Welcome to Guru99 Tutorials
การใช้ Python โมดูลระบบ
อีกวิธีหนึ่งที่คุณสามารถใช้เพื่อพิมพ์โดยไม่ต้องขึ้นบรรทัดใหม่ Python เป็นโมดูลในตัวที่เรียกว่า SYS.
นี่คือตัวอย่างการทำงานที่แสดงวิธีการใช้งาน ระบบ โมดูลที่จะพิมพ์โดยไม่ต้องขึ้นบรรทัดใหม่ Python เงื่อนไข
หากต้องการทำงานกับโมดูล sys อันดับแรก ให้นำเข้าโมดูล SYS ด้วย การนำเข้า คำสำคัญ. จากนั้นใช้เมธอด stdout.write() ที่มีอยู่ภายใน ระบบ โมดูลเพื่อพิมพ์สตริงของคุณ
import sys sys.stdout.write("Hello World ") sys.stdout.write("Welcome to Guru99 Tutorials")
Output:
Hello World Welcome to Guru99 Tutorials
การใช้ print() เพื่อพิมพ์รายการโดยไม่ต้องขึ้นบรรทัดใหม่
Consider a list of items for example: mylist = ["PHP", JAVA", "C++", "C", "PHYTHON"] and you want to print the values inside the list using สำหรับห่วง- ดังนั้นที่นี่คุณสามารถใช้ print() เพื่อแสดงค่าภายในรายการดังที่แสดงในตัวอย่างด้านล่าง:
mylist = ["PHP", "JAVA", "C++", "C", "PHYTHON"] for i in mylist: print(i)
Output:
PHP JAVA C++ C PHYTHON
ผลลัพธ์จะแสดงรายการต่างๆ ที่พิมพ์ออกมาทีละรายการในบรรทัดใหม่ จะทำอย่างไรถ้าคุณต้องการให้รายการทั้งหมดอยู่ในบรรทัดเดียวกัน? เพื่อสิ่งนั้น ให้ใช้อาร์กิวเมนต์ end ภายใน print() ที่จะลบบรรทัดใหม่ Python และพิมพ์รายการทั้งหมดในบรรทัดเดียวกัน
mylist = ["PHP", "JAVA", "C++", "C", "PYTHON"] for i in mylist: print(i, end=" ")
Output:
PHP JAVA C++ C PHYTHON
การพิมพ์รูปแบบดาว(*) โดยไม่ต้องขึ้นบรรทัดใหม่และเว้นวรรค
ตัวอย่างของ Python พิมพ์โดยไม่ต้องขึ้นบรรทัดใหม่จะใช้ประโยชน์จาก ฟังก์ชั่นการพิมพ์() การพิมพ์ดาว (*) บนบรรทัดเดียวกันโดยใช้ for-loop
for i in range(0, 20): print('*', end="")
Output:
********************
