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.
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
print
[
"Geeks : %2d, Portal : %5.2f"
%
[
%[flags][width][.precision]type1__12
print
[
%[flags][width][.precision]type7
%
[
I love Geeks for "Geeks!" Geeks and Portal Portal and Geeks0__122222214
print
[
I love Geeks for "Geeks!" Geeks and Portal Portal and Geeks6
%
[
I love Geeks for "Geeks!" Geeks and Portal Portal and Geeks9
%[flags][width][.precision]type4
print
[
Number one portal is Geeks, For, and Geeks. Geeks :12, Portal : 0.55 Second argument: 11, first one: 47.42 Geeks: 453, Portal: 59.063
%
[
Number one portal is Geeks, For, and Geeks. Geeks :12, Portal : 0.55 Second argument: 11, first one: 47.42 Geeks: 453, Portal: 59.066
%[flags][width][.precision]type4
Đầu ra: & nbsp;
Geeks : 1, Portal : 5.33 Total students : 240, Boys : 120 031 3.561E+02
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 –
Mã 1: & nbsp; & nbsp;
Python3
print
[
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal0
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal1
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal2
[
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal4
%[flags][width][.precision]type2
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal4
%[flags][width][.precision]type4
Các
print
[
print
0
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal1
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal2
[
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal4
%[flags][width][.precision]type2
Center aligned string with fillchr: ##########I love geeksforgeeks########## The left aligned string is : I love geeksforgeeks-------------------- The right aligned string is : --------------------I love geeksforgeeks6
%[flags][width][.precision]type4
print
print
9[
0[
1
print
print
9[
4[
1
Đầ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.
Code
2:
Python3
print
[
[
8
[
9
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal1
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal2
[
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal4
%[flags][width][.precision]type2
"Geeks : %2d, Portal : %5.2f"
5"Geeks : %2d, Portal : %5.2f"
6"Geeks : %2d, Portal : %5.2f"
7Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal4
%[flags][width][.precision]type4
print
[
%
2
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal1
%
4
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal2
[
%
7%[flags][width][.precision]type2
%
9%[flags][width][.precision]type4
print
[
%[flags][width][.precision]type03
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal1
%
4
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal2
[
%[flags][width][.precision]type08
%[flags][width][.precision]type2
%[flags][width][.precision]type10
%[flags][width][.precision]type4
print
[
%[flags][width][.precision]type14
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal1
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:
Python3
%[flags][width][.precision]type25
"Geeks : %2d, Portal : %5.2f"
7 %[flags][width][.precision]type27
%[flags][width][.precision]type28
%[flags][width][.precision]type29
%[flags][width][.precision]type30__12
print
[
%[flags][width][.precision]type42
%[flags][width][.precision]type43
%[flags][width][.precision]type44
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal1
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal2
%[flags][width][.precision]type47
%[flags][width][.precision]type48
"Geeks : %2d, Portal : %5.2f"
7 %[flags][width][.precision]type50
%[flags][width][.precision]type51
"Geeks : %2d, Portal : %5.2f"
7%[flags][width][.precision]type53
%[flags][width][.precision]type54
"Geeks : %2d, Portal : %5.2f"
7%[flags][width][.precision]type56
[
1print
[
%[flags][width][.precision]type60
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal1
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal2
[
%[flags][width][.precision]type64
%[flags][width][.precision]type64
%[flags][width][.precision]type66
Output:
Geeks: 4127; For: 4098; Geeks: 8637678 I love GeeksForGeeks computer Portal
Đị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[]
Python3
%[flags][width][.precision]type67
"Geeks : %2d, Portal : %5.2f"
7 %[flags][width][.precision]type69
print
[
%[flags][width][.precision]type72
[
1print
%[flags][width][.precision]type75
%[flags][width][.precision]type76
%[flags][width][.precision]type2
%[flags][width][.precision]type78
%[flags][width][.precision]type4
print
[
%[flags][width][.precision]type82
[
1print
%[flags][width][.precision]type85
%[flags][width][.precision]type76
%[flags][width][.precision]type2
%[flags][width][.precision]type88
%[flags][width][.precision]type4
print
[
%[flags][width][.precision]type92
[
1print
%[flags][width][.precision]type95
%[flags][width][.precision]type76
%[flags][width][.precision]type2
%[flags][width][.precision]type88
%[flags][width][.precision]type4
Đầu ra: & nbsp;
Center aligned string with fillchr: ##########I love geeksforgeeks########## The left aligned string is : I love geeksforgeeks-------------------- The right aligned string is : --------------------I love geeksforgeeks