ValueError:索引7处不支持格式字符“p”(0x70)

2024-10-03 09:09:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用一个循环来格式化字符串文件名这是我的代码

for i in range(1, 16):
    bgImageFile = ("bg_%01.png" %i)

Tags: 字符串代码inforpng文件名rangebg
1条回答
网友
1楼 · 发布于 2024-10-03 09:09:30

语法要求%d(或%s),而不仅仅是%

for i in range(1, 4):
    bgImageFile = 'bg_%s01.png'%i
    print(bgImageFile)

bg_101.png
bg_201.png
bg_301.png

在python3.6+中,可以使用f-strings(PEP498):

^{pr2}$

{你应该知道一些^说明符:

%s - String (or any object with a string representation, like numbers)

%d - Integers

%f - Floating point numbers

%.<number of digits>f - Floating point with fixed amount of digits to the right of the dot.

%x/%X - Integers in hex representation (lowercase/uppercase)

相关问题 更多 >