Python字符串格式化程序%

2024-06-16 10:40:16 发布

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

当字符串类似于:

s = 'test%s1'
s % 'TEST'

但是,当%本身出现在字符串中时,我得到了这个错误。 例如:

s = 'test%s12%34'
s % 'TEST'
>>ValueError: incomplete format

如何处理这个案子??你知道吗


Tags: 字符串testformat错误valueerrorincomplete案子s1
1条回答
网友
1楼 · 发布于 2024-06-16 10:40:16

加倍%

s = 'test%s12%%34'

在输出时,它将再次折叠为一个百分比符号:

>>> s = 'test%s12%%34'
>>> s % 'TEST'
'testTEST12%34'

String Formatting Operations documentation中,记录了各种转换字符:

'%' No argument is converted, results in a '%' character in the result.

其中第二个%是转换字符。你知道吗

相关问题 更多 >