英文:
How to format a string using variables instead of numbers within curly braces?
问题
我正在使用Python 3.7,并且已经将一个值存储在一个变量中。这个变量保存了我想要在字符串格式化的大括号内使用的填充值。下面的代码解释了我正在尝试做的事情。
def print_formatted(number):
for i in range(1, number + 1):
binum = bin(i).replace('0b', '')
ocnum = oct(i).replace('0o', '')
hexnum = hex(i).replace('0x', '')
length = len(bin(number).replace('0b', ''))
print('{0:>length} {1:>length} {2:>length} {3:>length}'.format(i, ocnum, hexnum, binum)) # 这里出错
这是我一直在尝试运行的代码。我想要做的是通过用最后一个二进制数字的长度进行填充来右对齐这些数字。
> ValueError: Invalid format specifier
这是我收到的错误。我做错了什么?
英文:
I'm using Python 3.7 and I have stored a value inside a variable. This variable holds the value of padding which I want to use within curly braces for string formatting. The code explains what I am trying to do.
def print_formatted(number):
for i in range(1, number + 1):
binum = bin(i).replace('0b', '')
ocnum = oct(i).replace('0o', '')
hexnum = hex(i).replace('0x', '')
length = len(bin(number).replace('0b', ''))
print('{0:>length} {1:>length} {2:>length} {3:>length}'.format(i, ocnum, hexnum, binum)) # Error here
This is the code that I have been trying to run. What I am trying to do is to right align the numbers by padding it by the value of the length of the last binary number.
> ValueError: Invalid format specifier
This is the error I get. What am I doing wrong?
答案1
得分: 3
你可以使用 f-strings 和格式说明符来避免使用 hex
、oct
和 bin
内置函数,然后使用字符串切片,使用 int.bit_length()
来代替获取二进制字符串长度,例如:
def print_formatted(number):
# 获取存储数字所需的位数
w = number.bit_length()
for n in range(1, number + 1):
# 打印每个数字的十进制、八进制、十六进制和二进制表示,并进行填充
print(f'{n:>{w}} {n:>{w}o} {n:>{w}x} {n:>{w}b}')
运行 print_formatted(20)
将会得到如下输出:
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4100
5 5 5 101
6 6 6 110
7 7 7 111
8 10 8 1000
9 11 9 1001
10 12 a 1010
11 13 b 1011
12 14 c 1100
13 15 d 1101
14 16 e 1110
15 17 f 1111
16 20 10 10000
17 21 11 10001
18 22 12 10010
19 23 13 10011
20 24 14 10100
英文:
You can use f-strings and also format specifiers to avoid use of the hex
, oct
and bin
builtins and then string slicing and use int.bit_length()
instead of taking the length of the binary string, eg:
def print_formatted(number):
# get number of bits required to store number
w = number.bit_length()
for n in range(1, number + 1):
# print each number as decimal, then octal, then hex, then binary with padding
print(f'{n:>{w}} {n:>{w}o} {n:>{w}x} {n:>{w}b}')
Running print_formatted(20)
will give you:
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 10 8 1000
9 11 9 1001
10 12 a 1010
11 13 b 1011
12 14 c 1100
13 15 d 1101
14 16 e 1110
15 17 f 1111
16 20 10 10000
17 21 11 10001
18 22 12 10010
19 23 13 10011
20 24 14 10100
答案2
得分: 2
你首先必须将变量 length
的值合并到你的格式字符串中,奇怪的是,使用 .format()
是最好的方法。
将以下行更改为:
print('{{0:>{length}}} {{1:>{length}}} {{2:>{length}}} {{3:>{length}}}'.format(length=length).format(i, ocnum, hexnum, binum))
英文:
You must first incorporate the value of the variable length
into your format string and, strangely, using .format()
is the best way to do it.
Change the following line
print('{0:>length} {1:>length} {2:>length} {3:>length}'.format(i, ocnum, hexnum, binum))
to
print('{{0:>{length}}} {{1:>{length}}} {{2:>{length}}} {{3:>{length}}}'.format(length=length).format(i, ocnum, hexnum, binum))
答案3
得分: 1
你可以使用f-strings
。
def print_formatted(number):
length = len(bin(number)) - 2
for i in range(1, number + 1):
ocnum = oct(i)[2:]
hexnum = hex(i)[2:]
binum = bin(i)[2:]
print(' '.join(f'{n:>{length}}' for n in (i, ocnum, hexnum, binum)))
>>> print_formatted(20)
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 10 8 1000
9 11 9 1001
10 12 a 1010
11 13 b 1011
12 14 c 1100
13 15 d 1101
14 16 e 1110
15 17 f 1111
16 20 10 10000
17 21 11 10001
18 22 12 10010
19 23 13 10011
20 24 14 10100
英文:
You can use f-strings
.
def print_formatted(number):
length = len(bin(number)) - 2
for i in range(1, number + 1):
ocnum = oct(i)[2:]
hexnum = hex(i)[2:]
binum = bin(i)[2:]
print(' '.join(f'{n:>{length}}' for n in (i, ocnum, hexnum, binum)))
>>> print_formatted(20)
1 1 1 1
2 2 2 10
3 3 3 11
4 4 4 100
5 5 5 101
6 6 6 110
7 7 7 111
8 10 8 1000
9 11 9 1001
10 12 a 1010
11 13 b 1011
12 14 c 1100
13 15 d 1101
14 16 e 1110
15 17 f 1111
16 20 10 10000
17 21 11 10001
18 22 12 10010
19 23 13 10011
20 24 14 10100
答案4
得分: 0
我通常分为两个步骤来做,首先创建一个格式字符串。它通过转义需要保留在格式字符串中的大括号 - {{
,}}
- 来工作。
>>> length = 4
>>> f'{{:>{length}}} {{:>{length}}} {{:>{length}}} {{:>{length}}}'
'{:>4} {:>4} {:>4} {:>4}'
>>>
fmt = f'{{:>{length}}} {{:>{length}}} {{:>{length}}} {{:>{length}}}'
print(fmt.format(i, ocnum, hexnum, binum))
英文:
I usually do it in two steps, creating a format string first. It works by escaping the braces - {{
,}}
- that need to remain in the format string.
>>> length = 4
>>> f'{{:>{length}}} {{:>{length}}} {{:>{length}}} {{:>{length}}}'
'{:>4} {:>4} {:>4} {:>4}'
>>>
fmt = f'{{:>{length}}} {{:>{length}}} {{:>{length}}} {{:>{length}}}'
print(fmt.format(i, ocnum, hexnum, binum))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论