Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

In nhiều dòng (Multi-Line printing) trong Python

Thường thấy lệnh print in một dòng. Nếu in nhiều dòng, cách thông thường cần nhiều lệnh print. Có thể tránh bằng kỹ thuật dùng ba dấu nháy đơn (''') như sau.

Ví dụ

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. ''')

Kết quả:

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.

Ví dụ: định dạng văn bản đẹp

print(''' ===========================================
 ||                                           ||
 || WORK                                      ||
 || HARD                                      ||
 ||                                           ||
 ||                                           ||
 ||                                           ||
 =========================================== ''')

Kết quả:

=============================
 ||                                           ||
 || WORK                                      ||
 || HARD                                      ||
 ||                                           ||
 ||                                           ||
 ||                                           ||
 =============================