You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 7.-input-and-output.md
+15-18Lines changed: 15 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,7 @@ The value of x is 32.5, and y is 40000...
38
38
"(32.5, 40000, ('spam', 'eggs'))"
39
39
```
40
40
41
-
Here are two ways to write a table of squares and cubes:>>>
41
+
Có 2 cách để in dữ liệu dạng bảng:>>>
42
42
43
43
```text
44
44
>>> for x in range(1, 11):
@@ -72,11 +72,11 @@ Here are two ways to write a table of squares and cubes:>>>
72
72
10 100 1000
73
73
```
74
74
75
-
\(Note that in the first example, one space between each column was added by the way [`print()`](https://docs.python.org/3/library/functions.html#print) works: by default it adds spaces between its arguments.\)
75
+
\(Lưu ý, trong ví dụ đầu tiên, nhận thấy giữa các cột phân cách nhau bởi 1 khoảng trắng, đó là do hàm [`print()`](https://docs.python.org/3/library/functions.html#print). Vì trong python, khi sử dụng hàm [`print()`](https://docs.python.org/3/library/functions.html#print), mặc định thêm 1 khoảng trắng giữa các tham số.\)
76
76
77
-
This example demonstrates the [`str.rjust()`](https://docs.python.org/3/library/stdtypes.html#str.rjust)method of string objects, which right-justifies a string in a field of a given width by padding it with spaces on the left. There are similar methods [`str.ljust()`](https://docs.python.org/3/library/stdtypes.html#str.ljust) and [`str.center()`](https://docs.python.org/3/library/stdtypes.html#str.center). These methods do not write anything, they just return a new string. If the input string is too long, they don’t truncate it, but return it unchanged; this will mess up your column lay-out but that’s usually better than the alternative, which would be lying about a value. \(If you really want truncation you can always add a slice operation, as in`x.ljust(n)[:n]`.\)
77
+
Ở ví dụ 1, hàm [`str.rjust()`](https://docs.python.org/3/library/stdtypes.html#str.rjust)căn phải 1 chuỗi theo độ rộng được cho, thêm khoảng trắng ở phía bên trái nếu cần thiết. Các hàm tương tự là: [`str.ljust()`](https://docs.python.org/3/library/stdtypes.html#str.ljust) and [`str.center()`](https://docs.python.org/3/library/stdtypes.html#str.center). Những hàm này không ghi ra bất kỳ thứ gì, chỉ đơn giản là trả về 1 chuỗi mới. Nếu chuỗi đầu vào quá dài, các hàm này không cắt chuỗi mà giữ nguyên chuỗi đó; như đã biết, việc giữ nguyên chuỗi quá dài khi in ra sẽ làm xáo trộn bố trí cột nhưng nó thường tốt hơn việc thay đổi chuỗi.\(Nếu thực sự muốn cắt chuỗi, có thể dùng các thao tác cắt chuỗi, ví dụ:`x.ljust(n)[:n]`.\)
78
78
79
-
There is another method, [`str.zfill()`](https://docs.python.org/3/library/stdtypes.html#str.zfill), which pads a numeric string on the left with zeros. It understands about plus and minus signs:>>>
79
+
Có 1 cách khác, dùng hàm [`str.zfill()`](https://docs.python.org/3/library/stdtypes.html#str.zfill), đệm một chuỗi số 0 về phía bên trái.
80
80
81
81
```text
82
82
>>> '12'.zfill(5)
@@ -87,14 +87,13 @@ There is another method, [`str.zfill()`](https://docs.python.org/3/library/stdty
87
87
'3.14159265359'
88
88
```
89
89
90
-
Basic usage of the [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format)method looks like this:>>>
90
+
Về cơ bản, cách sử dụng của hàm [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format)như sau:>>>
91
91
92
92
```text
93
93
>>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
94
94
We are the knights who say "Ni!"
95
95
```
96
-
97
-
The brackets and characters within them \(called format fields\) are replaced with the objects passed into the [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format) method. A number in the brackets can be used to refer to the position of the object passed into the [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format) method.>>>
96
+
Dầu ngặc {} và ký tự ở bên trong dấu ngoặc được thay thế bằng các tham số được truyền vào trong hàm [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format). Phần số ở trong dấu {} được sử dụng để chỉ ra vị trí in ra của tham số được truyền vào trong hàm [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format).>>>
98
97
99
98
```text
100
99
>>> print('{0} and {1}'.format('spam', 'eggs'))
@@ -103,23 +102,23 @@ spam and eggs
103
102
eggs and spam
104
103
```
105
104
106
-
If keyword arguments are used in the [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format) method, their values are referred to by using the name of the argument.>>>
105
+
Nếu tên tham số được sử dụng trong hàm [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format), giá trị của nó được in ra tại vị trí tương ứng với tên tham số đó.>>>
Positional and keyword arguments can be arbitrarily combined:>>>
113
+
Ví trí và tên tham số có thể được kết hợp tùy ý:>>>
115
114
116
115
```text
117
116
>>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
118
117
other='Georg'))
119
118
The story of Bill, Manfred, and Georg.
120
119
```
121
120
122
-
`'!a'`\(apply [`ascii()`](https://docs.python.org/3/library/functions.html#ascii)\), `'!s'`\(apply [`str()`](https://docs.python.org/3/library/stdtypes.html#str)\)and`'!r'`\(apply [`repr()`](https://docs.python.org/3/library/functions.html#repr)\)can be used to convert the value before it is formatted:>>>
121
+
`'!a'`\(áp dụng [`ascii()`](https://docs.python.org/3/library/functions.html#ascii)\), `'!s'`\(áp dụng [`str()`](https://docs.python.org/3/library/stdtypes.html#str)\)và`'!r'`\(áp dụng [`repr()`](https://docs.python.org/3/library/functions.html#repr)\)để chuyển đổi giá trị trước khi giá trị được định dạng:>>>
123
122
124
123
```text
125
124
>>> contents = 'eels'
@@ -129,15 +128,14 @@ My hovercraft is full of eels.
129
128
My hovercraft is full of 'eels'.
130
129
```
131
130
132
-
An optional `':'`and format specifier can follow the field name. This allows greater control over how the value is formatted. The following example rounds Pi to three places after the decimal.>>>
131
+
Có thể dùng `':'`và thông số định dạng(format specifier) ở trong {} để tùy chỉnh định dạng dữ liệu. Ví dụ bên dưới, làm tròn số Pi, lấy 3 chữ số sau dấu phẩy.>>>
133
132
134
133
```text
135
134
>>> import math
136
135
>>> print('The value of PI is approximately {0:.3f}.'.format(math.pi))
137
136
The value of PI is approximately 3.142.
138
137
```
139
-
140
-
Passing an integer after the `':'` will cause that field to be a minimum number of characters wide. This is useful for making tables pretty.>>>
138
+
Nếu thêm số nguyên sau dấu `':'` , định ra số ký tự tối thiếu được in ra. việc này rất có ý nghĩa khi in ra bảng(table).>>
If you have a really long format string that you don’t want to split up, it would be nice if you could reference the variables to be formatted by name instead of by position. This can be done by simply passing the dict and using square brackets `'[]'` to access the keys>>>
149
+
Nếu có 1 chuỗi rất dài, mà bạn không muốn chia chuỗi, có thể in các biến theo tên thay vì vị trí. Để làm điều đó, đưa vào 1 từ điển và sử dụng dấu ngoặc `'[]'` để truy cập các khóa của từ điển>>>
This is particularly useful in combination with the built-in function [`vars()`](https://docs.python.org/3/library/functions.html#vars), which returns a dictionary containing all local variables.
166
+
Điều này đặc biệt có ích trong việc kết hợp với hàm [`vars()`](https://docs.python.org/3/library/functions.html#vars), hàm này trả về một từ điển bao gồm tất cả các biến địa phương(local variables)
170
167
171
-
For a complete overview of string formatting with [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format), see[Format String Syntax](https://docs.python.org/3/library/string.html#formatstrings).
168
+
Để có một cái nhìn toàn diện về định dạng chuỗi với [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format), hãy xem chi tiết tại[Format String Syntax](https://docs.python.org/3/library/string.html#formatstrings).
0 commit comments