เรามักเห็นคำสั่ง print ใน Python พิมพ์ผลลัพธ์บรรทัดเดียว แต่ถ้ามีหลายบรรทัดที่ต้องพิมพ์ วิธีนี้ต้องเขียนคำสั่ง print หลายครั้ง สามารถหลีกเลี่ยงได้โดยใช้เทคนิคที่เกี่ยวข้องกับเครื่องหมายคำพูดเดี่ยว 3 ตัว ดังด้านล่าง
ตัวอย่าง
print('''Motivational Quote:
Sometimes later becomes never, Do it now.
Great things never come from comfort zones.
The harder you work for something, the greater you'll feel when you achieve it.''')
การรันโค้ดข้างต้นจะให้ผลลัพธ์ดังนี้:
Motivational Quote: Sometimes later becomes never, Do it now. Great things never come from comfort zones. The harder you work for something, the greater you'll feel when you achieve it.
ตัวอย่างเพิ่มเติม
เราสามารถใช้การพิมพ์หลายบรรทัดเพื่อสร้างข้อความจัดรูปแบบสวยงามได้อีกด้วย
ตัวอย่าง
print('''
===========================================
|| ||
|| WORK ||
|| HARD ||
|| ||
|| ||
|| ||
===========================================''')
การรันโค้ดข้างต้นจะให้ผลลัพธ์ดังนี้:
============================= || || || WORK || || HARD || || || || || || || =============================
สรุป: การใช้ triple quotes (''' หรือ """) เป็นวิธีที่ง่ายที่สุดสำหรับพิมพ์ข้อความหลายบรรทัดใน Python