[英]ValueError: unsupported format character 'p' (0x70) at index 7
I am trying to format the string file name by using a loop here is my code我正在尝试使用循环来格式化字符串文件名,这是我的代码
for i in range(1, 16):
bgImageFile = ("bg_%01.png" %i)
The syntax requires %d
(or %s
), not just %
:语法需要
%d
(或%s
),而不仅仅是%
:
for i in range(1, 4):
bgImageFile = 'bg_%s01.png'%i
print(bgImageFile)
bg_101.png
bg_201.png
bg_301.png
With Python 3.6+, you can use f-strings ( PEP498 ):在 Python 3.6+ 中,您可以使用 f-strings ( PEP498 ):
for i in range(1, 4):
bgImageFile = f'bg_{i}01.png'
Some specifiers you should know ( source ):您应该知道的一些说明符(来源):
%s
- String (or any object with a string representation, like numbers)%s
- 字符串(或任何具有字符串表示的对象,如数字)
%d
- Integers%d
- 整数
%f
- Floating point numbers%f
- 浮点数
%.<number of digits>f
- Floating point with fixed amount of digits to the right of the dot.%.<number of digits>f
- 点右侧具有固定位数的浮点数。
%x/%X
- Integers in hex representation (lowercase/uppercase)%x/%X
- 十六进制表示的整数(小写/大写)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.