Cách in nhiều câu trong Python

Chúng ta thường thấy lệnh in trong python in một dòng đầu ra. Nhưng nếu chúng ta có nhiều dòng để in, thì theo cách tiếp cận này, cần phải viết nhiều lệnh in. Điều này có thể tránh được bằng cách sử dụng một kỹ thuật khác liên quan đến ba trích dẫn đơn như bên dưới

Ví dụ

Bản thử trực tiếp

print['''
Motivational Quote :\n
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.
''']

Chạy đoạn mã trên cho ta kết quả như sau

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.

Chúng ta có thể sử dụng tính năng in nhiều dòng để tạo văn bản có định dạng độc đáo như bên dưới

Ví dụ

print['''
===========================================
||                                       ||
|| WORK                                  ||
|| HARD                                  ||
||                                       ||
||                                       ||
||                                       ||
===========================================
''']

Chạy đoạn mã trên cho ta kết quả như sau

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

Sử dụng chuỗi có ba dấu ngoặc kép để in nhiều dòng. Chuỗi trích dẫn ba lần cho phép chúng tôi xác định chuỗi nhiều dòng mà không cần thêm ký tự dòng mới

Chuỗi trích dẫn ba rất giống với chuỗi cơ bản mà chúng ta khai báo bằng cách sử dụng dấu ngoặc đơn hoặc dấu ngoặc kép

Nhưng chúng cũng cho phép chúng ta

  • sử dụng dấu ngoặc đơn và dấu ngoặc kép trong cùng một chuỗi mà không thoát
  • xác định chuỗi nhiều dòng mà không cần thêm ký tự dòng mới

Điều đầu tiên cần lưu ý khi sử dụng chuỗi nhiều dòng là thêm dấu gạch chéo ngược ở cuối dòng đầu tiên

Nếu bạn không thêm dấu gạch chéo ngược, bạn sẽ nhận thấy rằng một ký tự dòng mới bổ sung sẽ được thêm vào chuỗi

Đảm bảo đóng chuỗi nhiều dòng trên cùng một dòng

Nếu bạn không đóng chuỗi nhiều dòng trên cùng một dòng, một ký tự dòng mới bổ sung sẽ được thêm vào cuối chuỗi

Nếu bạn cần sử dụng các biến trong chuỗi nhiều dòng, hãy sử dụng ký tự chuỗi được định dạng ba dấu ngoặc kép

Chuỗi ký tự được định dạng [chuỗi f] cho phép chúng tôi bao gồm các biểu thức bên trong chuỗi bằng cách thêm tiền tố vào chuỗi bằng

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.
1

Đảm bảo ngắt các biểu thức trong dấu ngoặc nhọn -

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.
2

Ngoài ra, bạn có thể sử dụng thuộc tính

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.
3

In nhiều dòng văn bản bằng os. dòng chữ #

Sử dụng thuộc tính

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.
3 để in nhiều dòng. Thuộc tính
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.
3 trả về chuỗi được sử dụng để phân tách các dòng trên nền tảng hiện tại

hệ điều hành. thuộc tính linesep trả về chuỗi được sử dụng để phân tách các dòng trên nền tảng hiện tại

Ví dụ

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.
6 trên Unix và
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.
7 trên Windows

Chúng tôi đã sử dụng một chuỗi ký tự được định dạng trong ví dụ, nhưng bạn cũng có thể sử dụng đối số từ khóa

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.
8 của hàm
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.
9

Đối số

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.
8 là dấu phân cách giữa các đối số mà chúng ta chuyển đến
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.
9

Theo mặc định, đối số được đặt thành khoảng trắng

Bằng cách đặt đối số thành

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.
3, các giá trị chúng tôi chuyển đến
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.
9 được phân tách bằng ký tự dòng mới [
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.
6]

Ngoài ra, bạn có thể sử dụng phương pháp

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.
24

In nhiều dòng văn bản bằng cách sử dụng str. tham gia[] #

Sử dụng phương pháp

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.
24 để in nhiều dòng, e. g.
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.
26. Các mục trong danh sách sẽ được nối với dấu tách ký tự dòng mới [
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.
6] và sẽ được in trên các dòng riêng biệt

các str. phương thức tham gia lấy một iterable làm đối số và trả về một chuỗi là chuỗi nối của các chuỗi trong iterable

Lưu ý rằng phương thức này tăng

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.
28 nếu có bất kỳ giá trị không phải chuỗi nào trong lần lặp

Nếu iterable của bạn chứa số hoặc các loại khác, hãy chuyển đổi tất cả các giá trị thành chuỗi trước khi gọi

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.
29

ví dụ 1. Sử dụng dấu ngoặc kép

my_string = '''The only way to
learn to program is
by writing code.'''

print[my_string]

đầu ra

The only way to
learn to program is
by writing code.

Bạn có thể sử dụng

=============================
||                         ||
|| WORK                    ||
|| HARD                    ||
||                         ||
||                         ||
||                         ||
=============================
1 hoặc
=============================
||                         ||
|| WORK                    ||
|| HARD                    ||
||                         ||
||                         ||
||                         ||
=============================
2 để in chuỗi nhiều dòng như hình trên

ví dụ 2. Sử dụng dấu ngoặc đơn và dấu nháy đơn/kép

my_string = ["The only way to \n"
        	"learn to program is \n"
        	"by writing code."]

print[my_string]

đầu ra

The only way to
learn to program is
by writing code.

Nếu bạn sử dụng cú pháp

=============================
||                         ||
|| WORK                    ||
|| HARD                    ||
||                         ||
||                         ||
||                         ||
=============================
3, bạn cần chỉ định rõ ràng các dòng mới bằng cách sử dụng
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.
6

ví dụ 3. Sử dụng \

________số 8

đầu ra

The only way to
learn to program is
by writing code.

Bạn có thể sử dụng

The only way to
learn to program is
by writing code.
0 như trong đoạn mã ví dụ trên để viết một chuỗi nhiều dòng

Chủ Đề