Narnik Gamarnik Asked:2020-01-04 06:52:33 +0000 UTC2020-01-04 06:52:33 +0000 UTC 2020-01-04 06:52:33 +0000 UTC 用 f-string 替换 %-string 772 如何正确写表达式 "%(a)03d%(b)029d" % {"a": 1, "b": 2}, 使用f 字符串 python 1 个回答 Voted Best Answer MaxU - stop genocide of UA 2020-01-04T06:54:50Z2020-01-04T06:54:50Z In [3]: a,b = 1,2 In [4]: print(f"{a:03d}{b:029d}") 00100000000000000000000000000002 或者 In [5]: d = {"a": 1, "b": 2} In [6]: print(f"{d['a']:03d}{d['b']:029d}") 00100000000000000000000000000002 使用变量设置格式的示例: In [9]: fmt1,fmt2 = "03d", "029d" In [10]: print(f"{d['a']:{fmt1}}{d['b']:{fmt2}}") 00100000000000000000000000000002
或者
使用变量设置格式的示例: