Skip to content

Commit 4dc5fae

Browse files
authored
Update 7.-input-and-output.md
1 parent 9c297b3 commit 4dc5fae

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

7.-input-and-output.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
# 7. Input and Output
1+
# 7. Vào ra dữ liệu
22

3-
There are several ways to present the output of a program; data can be printed in a human-readable form, or written to a file for future use. This chapter will discuss some of the possibilities.
3+
Có một vài cách để thể hiện đầu ra của một chương trình: in dữ liệu ra màn hình dưới dạng con người có thể đọc được, hoặc ghi dữ liệu ra file để cho những mục đích sử dụng sau này.Trong chương này, sẽ để cập đến một vài cách xuất dữ liệu đầu ra.
44

5-
### 7.1. Fancier Output Formatting
5+
### 7.1. Định dạng dữ liệu đầu ra thường dùng
66

7-
So far we’ve encountered two ways of writing values: _expression statements_ and the [`print()`](https://docs.python.org/3/library/functions.html#print) function. \(A third way is using the `write()` method of file objects; the standard output file can be referenced as `sys.stdout`. See the Library Reference for more information on this.\)
7+
Hiện nay, có 2 cách để ghi giá trị: _các câu lệnh biểu diễn_ và hàm [`print()`](https://docs.python.org/3/library/functions.html#print). \(Cách thứ 3 là sử dụng phương thức `write()` của đối tượng file; chuẩn file đầu ra có thể được tham chiếu tại `sys.stdout`. Xem trong thư viện để biết thêm thông tin về `sys.stdout`.\)
8+
Thông thưrờng, các lập trình viên muốn tùy chỉnh hiển thị dữ liệu theo mục đích của mình chứ không đơn gian là in ra giá trị với dấu cách. Vì vậy, Python cung cấp 2 cách để định dạng dữ liệu đầu ra: Cách 1, người lập trình tự xử lý chuỗi dùng các thao tác cắt chuỗi và nối chuỗi. Với kiểu chuỗi(string), python cung cấp phương thức hữu hiệu để căn chỉnh chuỗi dựa theo chiều rộng cột; chúng ta sẽ thảo luận ở phần sau về vấn đề này.Cách 2, sử dụng [Định dạng chuỗi ký tự](https://docs.python.org/3/reference/lexical_analysis.html#f-strings), hoặc hàm [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format).
89

9-
Often you’ll want more control over the formatting of your output than simply printing space-separated values. There are two ways to format your output; the first way is to do all the string handling yourself; using string slicing and concatenation operations you can create any layout you can imagine. The string type has some methods that perform useful operations for padding strings to a given column width; these will be discussed shortly. The second way is to use [formatted string literals](https://docs.python.org/3/reference/lexical_analysis.html#f-strings), or the [`str.format()`](https://docs.python.org/3/library/stdtypes.html#str.format) method.
10+
Mô-đun [`string`](https://docs.python.org/3/library/string.html#module-string) bao gồm một lớp mẫu ([`Template`](https://docs.python.org/3/library/string.html#string.Template) class) thường có cách khác để chuyển đổi các giá trị thành chuỗi.
1011

11-
The [`string`](https://docs.python.org/3/library/string.html#module-string) module contains a [`Template`](https://docs.python.org/3/library/string.html#string.Template) class which offers yet another way to substitute values into strings.
12+
Một câu hỏi được đặt ra là: làm sao để chuyển từ giá trị thành chuỗi? Câu trả lời là, python đã cung cấp một số cách để chuyển từ bất kỳ giá trị nào về một chuỗi, sử dụng 2 hàm [`repr()`](https://docs.python.org/3/library/functions.html#repr) or [`str()`](https://docs.python.org/3/library/stdtypes.html#str).
1213

13-
One question remains, of course: how do you convert values to strings? Luckily, Python has ways to convert any value to a string: pass it to the [`repr()`](https://docs.python.org/3/library/functions.html#repr) or [`str()`](https://docs.python.org/3/library/stdtypes.html#str) functions.
14+
Hàm [`str()`](https://docs.python.org/3/library/stdtypes.html#str) trả về giá trị con người có thể đọc được, trong khi đó hàm [`repr()`](https://docs.python.org/3/library/functions.html#repr) trả về các giá trị đọc bởi trình thông dịch \(hoặc sẽ đẩy ra một lỗi ([`SyntaxError`](https://docs.python.org/3/library/exceptions.html#SyntaxError)) nếu không có cú pháp tương đương\). Với các đối tượng(object), hàm [`str()`](https://docs.python.org/3/library/stdtypes.html#str) sẽ trả về cũng một giá trị như hàm [`repr()`](https://docs.python.org/3/library/functions.html#repr). Nhiều giá trị, như số đếm hoặc dữ liệu cấu trúc như danh sách hoặc từ điển, giá trị trả về của 2 hàm [`str()`](https://docs.python.org/3/library/stdtypes.html#str) [`repr()`](https://docs.python.org/3/library/functions.html#repr) là như nhau. Đặc biệt các chuỗi, có 2 cách thể hiện riêng biệt.
1415

15-
The [`str()`](https://docs.python.org/3/library/stdtypes.html#str) function is meant to return representations of values which are fairly human-readable, while [`repr()`](https://docs.python.org/3/library/functions.html#repr) is meant to generate representations which can be read by the interpreter \(or will force a [`SyntaxError`](https://docs.python.org/3/library/exceptions.html#SyntaxError) if there is no equivalent syntax\). For objects which don’t have a particular representation for human consumption, [`str()`](https://docs.python.org/3/library/stdtypes.html#str) will return the same value as [`repr()`](https://docs.python.org/3/library/functions.html#repr). Many values, such as numbers or structures like lists and dictionaries, have the same representation using either function. Strings, in particular, have two distinct representations.
16-
17-
Some examples:>>>
16+
Một vài ví dụ:>>>
1817

1918
```text
2019
>>> s = 'Hello, world.'

0 commit comments

Comments
 (0)