Hướng dẫn python print fixed width - chiều rộng cố định in python

Chỉnh sửa 2013-12-11-Câu trả lời này rất cũ. Nó vẫn còn hợp lệ và chính xác, nhưng mọi người nhìn vào điều này nên thích cú pháp định dạng mới. - This answer is very old. It is still valid and correct, but people looking at this should prefer the new format syntax.

Bạn có thể sử dụng định dạng chuỗi như thế này:

>>> print '%5s' % 'aa'
   aa
>>> print '%5s' % 'aaa'
  aaa
>>> print '%5s' % 'aaaa'
 aaaa
>>> print '%5s' % 'aaaaa'
aaaaa

Basically:

  • Nhân vật % thông báo cho Python rằng nó sẽ phải thay thế một cái gì đó cho một mã thông báo
  • ký tự
    >>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
    >>> for item in dict_.items():
    ...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
    ... 
    value   a - num of occurances = 1
    value  ab - num of occurances = 1
    value abc - num of occurances = 1
    
    0 thông báo cho Python mã thông báo sẽ là một chuỗi
  • >>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
    >>> for item in dict_.items():
    ...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
    ... 
    value   a - num of occurances = 1
    value  ab - num of occurances = 1
    value abc - num of occurances = 1
    
    1 (hoặc bất kỳ số nào bạn muốn) thông báo cho Python để đệm chuỗi với khoảng trống lên đến 5 ký tự.

Trong trường hợp cụ thể của bạn, một triển khai có thể có vẻ như:

>>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
>>> for item in dict_.items():
...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
... 
value   a - num of occurances = 1
value  ab - num of occurances = 1
value abc - num of occurances = 1

Lưu ý bên cạnh: Chỉ tự hỏi nếu bạn biết về sự tồn tại của mô -đun

>>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
>>> for item in dict_.items():
...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
... 
value   a - num of occurances = 1
value  ab - num of occurances = 1
value abc - num of occurances = 1
2. Ví dụ: bạn có thể có được danh sách tất cả các kết hợp của bạn trong một dòng với: Just wondered if you are aware of the existence of the
>>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
>>> for item in dict_.items():
...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
... 
value   a - num of occurances = 1
value  ab - num of occurances = 1
value abc - num of occurances = 1
2 module. For example you could obtain a list of all your combinations in one line with:

>>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']

và bạn có thể nhận được số lần xuất hiện bằng cách sử dụng

>>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
>>> for item in dict_.items():
...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
... 
value   a - num of occurances = 1
value  ab - num of occurances = 1
value abc - num of occurances = 1
3 kết hợp với
>>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
>>> for item in dict_.items():
...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
... 
value   a - num of occurances = 1
value  ab - num of occurances = 1
value abc - num of occurances = 1
4.

Định dạng một số đến chiều rộng cố định trong Python #

Sử dụng một chuỗi được định dạng theo nghĩa đen để định dạng một số theo chiều rộng cố định, ví dụ:

>>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
>>> for item in dict_.items():
...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
... 
value   a - num of occurances = 1
value  ab - num of occurances = 1
value abc - num of occurances = 1
5. Chuỗi được định dạng theo nghĩa đen sẽ định dạng số theo chiều rộng được chỉ định bằng cách tiền tố nó với số lượng các số không cần thiết.

Copied!

my_int = 12 # ✅ format integer to fixed width of 3 result = f'{my_int:03d}' print(result) # 👉️ '012' # ✅ format integer to fixed width of 4 result = f'{my_int:04d}' print(result) # 👉️ '0012' # ✅ format integer to fixed width of 3 (with leading spaces) result = f'{my_int:3}' print(repr(result)) # 👉️ ' 12' # ----------------------------------- my_float = 1.234567 # ✅ format float to fixed width of 8 with 3 digits after the decimal result = f'{my_float:8.3f}' print(repr(result)) # 👉️ ' 1.235' # ----------------------------------- # ✅ using str.zfill() result = str(my_int).zfill(3) print(result) # 👉️ '012' result = str(my_int).zfill(4) print(result) # 👉️ '0012'

Ví dụ đầu tiên sử dụng một chuỗi được định dạng theo nghĩa đen để định dạng một số theo chiều rộng cố định.

Copied!

my_int = 12 result = f'{my_int:03d}' print(result) # 👉️ '012' result = f'{my_int:04d}' print(result) # 👉️ '0012'

Các định dạng chuỗi F đầu tiên số lượng đến chiều rộng 3 chữ số và chữ số thứ hai đến chiều rộng 4 chữ số.

Nếu cần thiết, các số 0 hàng đầu được thêm vào định dạng số vào chiều rộng được chỉ định.

Các chuỗi chữ được định dạng (F-Strings) Hãy cho chúng tôi bao gồm các biểu thức bên trong chuỗi bằng cách tiền tố chuỗi với

>>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
>>> for item in dict_.items():
...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
... 
value   a - num of occurances = 1
value  ab - num of occurances = 1
value abc - num of occurances = 1
6.

Copied!

my_str = 'The number is:' my_int = 5000 result = f'{my_str} {my_int}' print(result) # 👉️ The number is: 5000

Hãy chắc chắn để bọc các biểu thức trong niềng răng xoăn -

>>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
>>> for item in dict_.items():
...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
... 
value   a - num of occurances = 1
value  ab - num of occurances = 1
value abc - num of occurances = 1
7.

Bạn cũng có thể sử dụng một chuỗi được định dạng theo nghĩa đen nếu bạn cần định dạng một chiếc phao theo chiều rộng cố định.

Copied!

my_float = 1.234567 # ✅ format float to fixed length of 8 with 3 digits after the decimal result = f'{my_float:8.3f}' print(repr(result)) # 👉️ ' 1.235'

Ví dụ định dạng phao nổi theo chiều rộng

>>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
>>> for item in dict_.items():
...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
... 
value   a - num of occurances = 1
value  ab - num of occurances = 1
value abc - num of occurances = 1
8 với 3 chữ số sau thập phân.

Chuỗi được gắn bên trái với khoảng trắng để định dạng phao theo chiều dài được chỉ định.

Định dạng một số đến chiều rộng cố định bằng cách sử dụng str.zfill () #

Để định dạng một số theo chiều rộng cố định:

  1. Sử dụng lớp
    >>> dict_ = {'a': 1, 'ab': 1, 'abc': 1}
    >>> for item in dict_.items():
    ...     print 'value %3s - num of occurances = %d' % item # %d is the token of integers
    ... 
    value   a - num of occurances = 1
    value  ab - num of occurances = 1
    value abc - num of occurances = 1
    
    9 để chuyển đổi số thành chuỗi.
  2. Sử dụng phương thức
    >>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
    ['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']
    
    0 để định dạng chuỗi theo chiều rộng được chỉ định.
  3. Phương pháp
    >>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
    ['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']
    
    1 bên trái điền vào chuỗi với
    >>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
    ['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']
    
    2 chữ số để làm cho nó có độ dài được chỉ định.

Copied!

my_int = 12 result = str(my_int).zfill(3) print(result) # 👉️ '012' result = str(my_int).zfill(4) print(result) # 👉️ '0012'

Phương thức str.zfill lấy chiều rộng của chuỗi và bên trái đổ chuỗi bằng

>>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']
2 chữ số để làm cho nó có chiều rộng được chỉ định.

Chuyển đổi số

>>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']
4 thành một chuỗi cung cấp cho chúng tôi một chuỗi với độ dài
>>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']
5.

Vượt qua

>>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']
6 làm chiều rộng cho phương thức
>>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']
1 có nghĩa là chuỗi sẽ được chứa đầy bên trái với một chữ số
>>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']
2.

Phương pháp

>>> [''.join(perm) for i in range(1, len(s)) for perm in it.permutations(s, i)]
['a', 'b', 'c', 'd', 'ab', 'ac', 'ad', 'ba', 'bc', 'bd', 'ca', 'cb', 'cd', 'da', 'db', 'dc', 'abc', 'abd', 'acb', 'acd', 'adb', 'adc', 'bac', 'bad', 'bca', 'bcd', 'bda', 'bdc', 'cab', 'cad', 'cba', 'cbd', 'cda', 'cdb', 'dab', 'dac', 'dba', 'dbc', 'dca', 'dcb']
0 xử lý một tiền tố dấu hiệu hàng đầu (ví dụ:

Copied!

my_int = 12 # ✅ format integer to fixed width of 3 result = f'{my_int:03d}' print(result) # 👉️ '012' # ✅ format integer to fixed width of 4 result = f'{my_int:04d}' print(result) # 👉️ '0012' # ✅ format integer to fixed width of 3 (with leading spaces) result = f'{my_int:3}' print(repr(result)) # 👉️ ' 12' # ----------------------------------- my_float = 1.234567 # ✅ format float to fixed width of 8 with 3 digits after the decimal result = f'{my_float:8.3f}' print(repr(result)) # 👉️ ' 1.235' # ----------------------------------- # ✅ using str.zfill() result = str(my_int).zfill(3) print(result) # 👉️ '012' result = str(my_int).zfill(4) print(result) # 👉️ '0012'
0 hoặc

Copied!

my_int = 12 # ✅ format integer to fixed width of 3 result = f'{my_int:03d}' print(result) # 👉️ '012' # ✅ format integer to fixed width of 4 result = f'{my_int:04d}' print(result) # 👉️ '0012' # ✅ format integer to fixed width of 3 (with leading spaces) result = f'{my_int:3}' print(repr(result)) # 👉️ ' 12' # ----------------------------------- my_float = 1.234567 # ✅ format float to fixed width of 8 with 3 digits after the decimal result = f'{my_float:8.3f}' print(repr(result)) # 👉️ ' 1.235' # ----------------------------------- # ✅ using str.zfill() result = str(my_int).zfill(3) print(result) # 👉️ '012' result = str(my_int).zfill(4) print(result) # 👉️ '0012'
1) bằng cách chèn đệm sau dấu hiệu.

Copied!

num = -12 result_1 = str(num).zfill(3) print(result_1) # 👉️ '-12' result_2 = str(num).zfill(4) print(result_2) # 👉️ '-012'

Lưu ý rằng dấu hiệu được tính theo chiều rộng của chuỗi.

Nếu chiều rộng được chỉ định nhỏ hơn hoặc bằng chiều dài của chuỗi gốc, thì chuỗi ban đầu được trả về.