Skip to content

Commit 9a65024

Browse files
authored
Update 7.-input-and-output.md
1 parent b11a465 commit 9a65024

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

7.-input-and-output.md

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ The value of x is 32.5, and y is 40000...
3838
"(32.5, 40000, ('spam', 'eggs'))"
3939
```
4040

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:>>>
4242

4343
```text
4444
>>> for x in range(1, 11):
@@ -72,11 +72,11 @@ Here are two ways to write a table of squares and cubes:>>>
7272
10 100 1000
7373
```
7474

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ố.\)
7676

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]`.\)
7878

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

8181
```text
8282
>>> '12'.zfill(5)
@@ -87,14 +87,13 @@ There is another method, [`str.zfill()`](https://docs.python.org/3/library/stdty
8787
'3.14159265359'
8888
```
8989

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:>>>
9191

9292
```text
9393
>>> print('We are the {} who say "{}!"'.format('knights', 'Ni'))
9494
We are the knights who say "Ni!"
9595
```
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).>>>
9897

9998
```text
10099
>>> print('{0} and {1}'.format('spam', 'eggs'))
@@ -103,23 +102,23 @@ spam and eggs
103102
eggs and spam
104103
```
105104

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ố đó.>>>
107106

108107
```text
109108
>>> print('This {food} is {adjective}.'.format(
110109
... food='spam', adjective='absolutely horrible'))
111110
This spam is absolutely horrible.
112111
```
113112

114-
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 ý:>>>
115114

116115
```text
117116
>>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
118117
other='Georg'))
119118
The story of Bill, Manfred, and Georg.
120119
```
121120

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)\) `'!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:>>>
123122

124123
```text
125124
>>> contents = 'eels'
@@ -129,15 +128,14 @@ My hovercraft is full of eels.
129128
My hovercraft is full of 'eels'.
130129
```
131130

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

134133
```text
135134
>>> import math
136135
>>> print('The value of PI is approximately {0:.3f}.'.format(math.pi))
137136
The value of PI is approximately 3.142.
138137
```
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).>>
141139

142140
```text
143141
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
@@ -148,8 +146,7 @@ Jack ==> 4098
148146
Dcab ==> 7678
149147
Sjoerd ==> 4127
150148
```
151-
152-
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>>>
153150

154151
```text
155152
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
@@ -158,17 +155,17 @@ If you have a really long format string that you don’t want to split up, it wo
158155
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
159156
```
160157

161-
This could also be done by passing the table as keyword arguments with the \*\*notation.>>>
158+
Có thể đưa table là một tham số và thêm 2 dấu \*\*ở đầu.>>>
162159

163160
```text
164161
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
165162
>>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table))
166163
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
167164
```
168165

169-
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)
170167

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

173170
#### 7.1.1. Old string formatting
174171

0 commit comments

Comments
 (0)