[英]Can f-strings auto-pad to [the next] even number of digits on output?
Based on this answer (among others) it seems like f-strings is [one of] the preferred ways to convert to hexadecimal representation.基于这个答案(除其他外),f-strings 似乎是转换为十六进制表示的首选方法之一。
While one can specify an explicit target length, up to which to pad with leading zeroes, given a goal of an output with an even number of digits , and inputs with an arbitrary # of bits, I can imagine:虽然可以指定一个明确的目标长度,最多可以用前导零填充,但考虑到 output 的目标是偶数位数和任意位数的输入,我可以想象:
out = "0"+f"{val:x}" if len(f"{val:x}") % 2 else f"{val:02x}"
(or even using .zfill()
)后处理 a-la out = "0"+f"{val:x}" if len(f"{val:x}") % 2 else f"{val:02x}"
(甚至使用.zfill()
)The latter seems like it might be more efficient than the former - is there a built-in way to do this with fstrings, or a better alternative?后者似乎比前者更有效 - 是否有使用 fstrings 的内置方法或更好的替代方法?
Examples of input + expected output:输入示例 + 预期 output:
[0x]1 -> [0x]01
[0x]22 -> [0x]22
[0x]333 -> [0x]0333
[0x]4444 -> [0x]4444
and so on.等等。
You can use a variable in the pad-length part of the f-string.您可以在 f 字符串的填充长度部分使用变量。 For example:例如:
n = 4
val = 257
print(f"{val:0{n}x}") # 0101
Now, to figure out how many hex characters are in an integer, you just need to find how many bits are in the integer:现在,要弄清楚 integer 中有多少个十六进制字符,您只需要找出 integer 中有多少位:
hex_count, rem = divmod(max(1, val.bit_length()), 4)
hex_count += (rem > 0)
( max(1, val.bit_length())
handles the case where val == 0
, which has a bit length of 0) ( max(1, val.bit_length())
处理val == 0
的情况,其位长为 0)
So let's get the next even number after hex_count
:所以让我们得到hex_count
之后的下一个偶数:
pad_length = hex_count + (hex_count % 2)
print(f"{val:0{pad_length}x}") # 0101
I'm not sure if this is any better than simply converting it to a hex string and then figuring out how much padding is needed, but I can't think of a readable way to do this all in an f-string.我不确定这是否比简单地将其转换为十六进制字符串然后确定需要多少填充更好,但我想不出一种可读的方法来在 f 字符串中完成这一切。 An unreadable way would be by combining all of the above into a single line, but IMO readable code is better than unreadable one-liners.一种不可读的方法是将上述所有内容组合成一行,但 IMO 可读代码比不可读的单行代码更好。 I don't think there's a way to specify what you want as a simple f-string.我认为没有办法将您想要的内容指定为简单的 f 字符串。
Note that negative numbers are formatted to an even number of digits, plus the -
sign.请注意,负数被格式化为偶数位数,加上-
符号。
I don't think there's anything built in to f-string formatting that will do this.我认为 f-string 格式没有内置任何东西可以做到这一点。 You probably have to figure out what the "natural" width would be then round that up to the next even number.您可能必须弄清楚“自然”宽度是多少,然后将其四舍五入到下一个偶数。
Something like this:像这样的东西:
def hf(n):
width = len(hex(n)) - 2 # account for leading 0x
width += width % 2 # round up
return f'{n:0{width}x}'
print(hf(1))
print(hf(15))
print(hf(16))
print(hf(255))
print(hf(256))
Output: Output:
01
0f
10
ff
0100
Here's a postprocessing alternative that uses assignment expressions (Python 3.8+):这是使用赋值表达式(Python 3.8+)的后处理替代方案:
print((len(hx:=f"{val:x}") % 2) * '0' + hx)
If you still want a one-liner without assignment expressions you have to evaluate your f-string twice:如果你仍然想要一个没有赋值表达式的单行,你必须计算你的 f-string 两次:
print((len(f"{val:x}") % 2) * '0' + f"{val:x}")
As a two-liner作为双排
hx = f"{val:x}"
print((len(hx) % 2) * '0' + hx)
And one more version:还有一个版本:
print(f"{'0'[:len(hex(val))%2]}{val:x}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.