Hướng dẫn formatting output in python - định dạng đầu ra trong python

Có một số cách để trình bày đầu ra của một chương trình. Dữ liệu có thể được in dưới dạng người có thể đọc được hoặc được ghi vào một tệp để sử dụng trong tương lai hoặc thậm chí trong một số hình thức được chỉ định khác. Người dùng thường muốn kiểm soát nhiều hơn về định dạng đầu ra hơn là chỉ đơn giản là in các giá trị phân tách không gian. Có một số cách để định dạng đầu ra. & NBSP;

  • Để sử dụng các chuỗi chữ được định dạng, hãy bắt đầu một chuỗi với F hoặc F trước dấu ngoặc kép mở hoặc dấu trích báo ba.
  • STR. Định dạng () Phương thức chuỗi giúp người dùng tạo đầu ra fancier
  • Người dùng có thể thực hiện tất cả các xử lý chuỗi bằng cách sử dụng các hoạt động cắt và nối chuỗi để tạo bất kỳ bố cục nào mà người dùng muốn. Loại chuỗi có một số phương thức thực hiện các hoạt động hữu ích cho các chuỗi đệm theo chiều rộng cột nhất định.

Định dạng đầu ra bằng toán tử modulo chuỗi ( %): & nbsp; toán tử % cũng có thể được sử dụng cho định dạng chuỗi. Nó diễn giải đối số bên trái giống như định dạng kiểu printf ()-như trong các chuỗi ngôn ngữ C sẽ được áp dụng cho đối số đúng. Trong Python, không có hàm printf () nhưng chức năng của printf cổ đại được chứa trong Python. Với mục đích này, toán tử modulo % bị quá tải bởi lớp chuỗi để thực hiện định dạng chuỗi. Do đó, nó thường được gọi là toán tử Modulo (hoặc đôi khi thậm chí được gọi là mô đun). & NBSP; The % operator can also be used for string formatting. It interprets the left argument much like a printf()-style format as in C language strings to be applied to the right argument. In Python, there is no printf() function but the functionality of the ancient printf is contained in Python. To this purpose, the modulo operator % is overloaded by the string class to perform string formatting. Therefore, it is often called a string modulo (or sometimes even called modulus) operator.  
The % operator can also be used for string formatting. It interprets the left argument much like a printf()-style format as in C language strings to be applied to the right argument. In Python, there is no printf() function but the functionality of the ancient printf is contained in Python. To this purpose, the modulo operator % is overloaded by the string class to perform string formatting. Therefore, it is often called a string modulo (or sometimes even called modulus) operator. 

Toán tử modulo chuỗi ( %) vẫn có sẵn trong Python (3.x) và được sử dụng rộng rãi. Nhưng ngày nay, phong cách định dạng cũ được xóa khỏi ngôn ngữ. & NBSP;

Python3

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
2
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
3
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1

 %[flags][width][.precision]type 
1__12

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1

 %[flags][width][.precision]type 
7
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
3
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1
I love Geeks for "Geeks!"
Geeks and Portal
Portal and Geeks
0__122222214

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1

I love Geeks for "Geeks!"
Geeks and Portal
Portal and Geeks
6
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
3
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1
I love Geeks for "Geeks!"
Geeks and Portal
Portal and Geeks
9
 %[flags][width][.precision]type 
4

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1

Number one portal is Geeks, For, and Geeks.
Geeks :12, Portal :    0.55
Second argument:  11, first one:   47.42
Geeks:   453, Portal:    59.06
3
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
3
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1
Number one portal is Geeks, For, and Geeks.
Geeks :12, Portal :    0.55
Second argument:  11, first one:   47.42
Geeks:   453, Portal:    59.06
6
 %[flags][width][.precision]type 
4

Đầu ra: & nbsp;  

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02

Hướng dẫn formatting output in python - định dạng đầu ra trong python

Có hai trong số những người trong ví dụ của chúng tôi: Phần trăm%2D và%%5.2F. Cú pháp chung cho một trình giữ chỗ định dạng là: & nbsp;

 %[flags][width][.precision]type 

Chúng ta hãy xem xét các chủ sở hữu chỗ trong ví dụ của chúng tôi. & Nbsp; & nbsp;

  • Người giữ chỗ đầu tiên,%2D 2D được sử dụng cho thành phần đầu tiên của bộ tuple của chúng tôi, tức là số nguyên 1. Số sẽ được in với 2 ký tự. Vì 1 chỉ bao gồm một chữ số, đầu ra được đệm với 1 khoảng trống hàng đầu.
  • Cái thứ hai%%5.2F là một mô tả định dạng cho số float. Giống như những người giữ chỗ khác, nó được giới thiệu với nhân vật %. Tiếp theo là tổng số chữ số mà chuỗi phải chứa. Con số này bao gồm điểm thập phân và tất cả các chữ số, tức là trước và sau điểm thập phân.
  • Số float của chúng tôi 05.333 phải được định dạng với 5 ký tự. Phần thập phân của số hoặc độ chính xác được đặt thành 2, tức là số theo sau. trong trình giữ chỗ của chúng tôi. Cuối cùng, nhân vật cuối cùng của F F của người giữ chỗ của chúng tôi là viết tắt của Float Float.

Định dạng đầu ra bằng phương thức định dạng: & nbsp; Phương thức định dạng () được thêm vào python (2.6). Phương pháp định dạng của chuỗi đòi hỏi nhiều nỗ lực thủ công hơn. Người dùng sử dụng {} để đánh dấu nơi một biến sẽ được thay thế và có thể cung cấp các chỉ thị định dạng chi tiết, nhưng người dùng cũng cần cung cấp thông tin được định dạng. Phương pháp này cho phép chúng ta nối các phần tử trong đầu ra thông qua định dạng vị trí. Ví dụ - & nbsp; The format() method was added in Python(2.6). The format method of strings requires more manual effort. Users use {} to mark where a variable will be substituted and can provide detailed formatting directives, but the user also needs to provide the information to be formatted. This method lets us concatenate elements within an output through positional formatting. For Example –  
The format() method was added in Python(2.6). The format method of strings requires more manual effort. Users use {} to mark where a variable will be substituted and can provide detailed formatting directives, but the user also needs to provide the information to be formatted. This method lets us concatenate elements within an output through positional formatting. For Example – 

Mã 1: & nbsp; & nbsp;    

Python3

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1

 %[flags][width][.precision]type 
10
 %[flags][width][.precision]type 
11
 %[flags][width][.precision]type 
12
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1
 %[flags][width][.precision]type 
14
 %[flags][width][.precision]type 
2
 %[flags][width][.precision]type 
14
 %[flags][width][.precision]type 
4

Các

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
00

 %[flags][width][.precision]type 
11
 %[flags][width][.precision]type 
12
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1
 %[flags][width][.precision]type 
14
 %[flags][width][.precision]type 
2
I love Geeks for "Geeks!"
Geeks and Portal
Portal and Geeks
26
 %[flags][width][.precision]type 
4

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
09
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
10
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
11

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
09
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
14
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
11

Đầu ra: & nbsp;  

I love Geeks for "Geeks!"
Geeks and Portal
Portal and Geeks

Có hai trong số những người trong ví dụ của chúng tôi: Phần trăm%2D và%%5.2F. Cú pháp chung cho một trình giữ chỗ định dạng là: & nbsp;format fields) are replaced with the objects passed into the format() method. A number in the brackets can be used to refer to the position of the object passed into the format() method.format fields) are replaced with the objects passed into the format() method. A number in the brackets can be used to refer to the position of the object passed into the format() method.

  Code 2:Code 2:

Python3

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
18

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
19

 %[flags][width][.precision]type 
11
 %[flags][width][.precision]type 
12
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1
 %[flags][width][.precision]type 
14
 %[flags][width][.precision]type 
2
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
25
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
26
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
27
 %[flags][width][.precision]type 
14
 %[flags][width][.precision]type 
4

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
32

 %[flags][width][.precision]type 
11

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
34

 %[flags][width][.precision]type 
12
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
37
 %[flags][width][.precision]type 
2
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
39
 %[flags][width][.precision]type 
4

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1

 %[flags][width][.precision]type 
03
 %[flags][width][.precision]type 
11

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
34

 %[flags][width][.precision]type 
12
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1
 %[flags][width][.precision]type 
08
 %[flags][width][.precision]type 
2
 %[flags][width][.precision]type 
10
 %[flags][width][.precision]type 
4

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1

 %[flags][width][.precision]type 
14
I love Geeks for "Geeks!"
Geeks and Portal
Portal and Geeks
31

Chúng ta hãy xem xét các chủ sở hữu chỗ trong ví dụ của chúng tôi. & Nbsp; & nbsp;

Output:   

Number one portal is Geeks, For, and Geeks.
Geeks :12, Portal :    0.55
Second argument:  11, first one:   47.42
Geeks:   453, Portal:    59.06

Người giữ chỗ đầu tiên,%2D 2D được sử dụng cho thành phần đầu tiên của bộ tuple của chúng tôi, tức là số nguyên 1. Số sẽ được in với 2 ký tự. Vì 1 chỉ bao gồm một chữ số, đầu ra được đệm với 1 khoảng trống hàng đầu.

& nbsp; & nbsp; mã 3: & nbsp;Code 3: 
Code 3: 

Python3

 %[flags][width][.precision]type 
25
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
27
 %[flags][width][.precision]type 
27
 %[flags][width][.precision]type 
28
 %[flags][width][.precision]type 
29
 %[flags][width][.precision]type 
30__12

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1

 %[flags][width][.precision]type 
42
 %[flags][width][.precision]type 
43
 %[flags][width][.precision]type 
44
 %[flags][width][.precision]type 
11
 %[flags][width][.precision]type 
12
 %[flags][width][.precision]type 
47
 %[flags][width][.precision]type 
48
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
27
 %[flags][width][.precision]type 
50
 %[flags][width][.precision]type 
51
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
27
 %[flags][width][.precision]type 
53
 %[flags][width][.precision]type 
54
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
27
 %[flags][width][.precision]type 
56
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
11

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1

 %[flags][width][.precision]type 
60
 %[flags][width][.precision]type 
11
 %[flags][width][.precision]type 
12
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1
 %[flags][width][.precision]type 
64
 %[flags][width][.precision]type 
64
 %[flags][width][.precision]type 
66

Output:   

 %[flags][width][.precision]type 
1

Định dạng đầu ra bằng phương thức chuỗi: & nbsp; đầu ra này được định dạng bằng cách sử dụng các hoạt động cắt và nối chuỗi. Loại chuỗi có một số phương thức giúp định dạng đầu ra theo cách fancier. Một số phương pháp giúp định dạng đầu ra là str.ljust (), str.rjust () và str.centre () This output is formatted by using string slicing and concatenation operations. The string type has some methods that help in formatting output in a fancier way. Some methods which help in formatting an output are str.ljust(), str.rjust(), and str.centre() 
This output is formatted by using string slicing and concatenation operations. The string type has some methods that help in formatting output in a fancier way. Some methods which help in formatting an output are str.ljust(), str.rjust(), and str.centre()

Python3

 %[flags][width][.precision]type 
67
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
27
 %[flags][width][.precision]type 
69

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1

 %[flags][width][.precision]type 
72
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
11

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0

 %[flags][width][.precision]type 
75
 %[flags][width][.precision]type 
76
 %[flags][width][.precision]type 
2
 %[flags][width][.precision]type 
78
 %[flags][width][.precision]type 
4

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1

 %[flags][width][.precision]type 
82
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
11

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0

 %[flags][width][.precision]type 
85
 %[flags][width][.precision]type 
76
 %[flags][width][.precision]type 
02
 %[flags][width][.precision]type 
88
 %[flags][width][.precision]type 
4

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
1

 %[flags][width][.precision]type 
92
Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
11

Geeks :  1, Portal : 5.33
Total students : 240, Boys : 120
    031
3.561E+02
0

 %[flags][width][.precision]type 
95
 %[flags][width][.precision]type 
76
 %[flags][width][.precision]type 
2
 %[flags][width][.precision]type 
88
 %[flags][width][.precision]type 
4

Đầu ra: & nbsp;   

I love Geeks for "Geeks!"
Geeks and Portal
Portal and Geeks
2Định dạng đầu ra là gì?

Định dạng đầu ra là gì?

Một số chức năng thư viện giúp bạn chuyển đổi các giá trị dữ liệu từ các biểu diễn nội bộ được mã hóa sang các chuỗi văn bản thường có thể đọc được bởi mọi người.Bạn cung cấp một chuỗi định dạng dưới dạng giá trị của đối số định dạng cho từng hàm này, do đó thuật ngữ đầu ra được định dạng.convert data values from encoded internal representations to text sequences that are generally readable by people. You provide a format string as the value of the format argument to each of these functions, hence the term formatted output.convert data values from encoded internal representations to text sequences that are generally readable by people. You provide a format string as the value of the format argument to each of these functions, hence the term formatted output.

Định dạng trong Python là gì?

Định dạng chuỗi còn được gọi là nội suy chuỗi.Đó là quá trình chèn một chuỗi tùy chỉnh hoặc biến trong văn bản được xác định trước.Custom_String = "Định dạng chuỗi" PRINT (F "{Custom_String} là một kỹ thuật mạnh mẽ") được cung cấp bởi không gian làm việc của DataCamp.Định dạng chuỗi là một kỹ thuật mạnh mẽ.the process of inserting a custom string or variable in predefined text. custom_string = "String formatting" print(f"{custom_string} is a powerful technique") Powered by Datacamp Workspace. String formatting is a powerful technique.the process of inserting a custom string or variable in predefined text. custom_string = "String formatting" print(f"{custom_string} is a powerful technique") Powered by Datacamp Workspace. String formatting is a powerful technique.

%S và %d có nghĩa là gì trong Python?

%s hoạt động một trình giữ chỗ cho một chuỗi trong khi %D hoạt động như một trình giữ chỗ cho một số.Các giá trị liên quan của chúng được truyền qua thông qua một tuple bằng toán tử %.. Their associated values are passed in via a tuple using the % operator.. Their associated values are passed in via a tuple using the % operator.