Sau*_*rma 0 python string-formatting
当字符串类似于以下内容时,这很简单:
s = 'test%s1'
s % 'TEST'
Run Code Online (Sandbox Code Playgroud)
但是,当%它本身存在于字符串中时,我就会收到此错误.例如:
s = 'test%s12%34'
s % 'TEST'
>>ValueError: incomplete format
Run Code Online (Sandbox Code Playgroud)
如何处理此案?
加倍%:
s = 'test%s12%%34'
Run Code Online (Sandbox Code Playgroud)
在输出时,它将再次折叠为单个百分比符号:
>>> s = 'test%s12%%34'
>>> s % 'TEST'
'testTEST12%34'
Run Code Online (Sandbox Code Playgroud)
从字符串格式化操作文档中,记录了各种转换字符:
'%'不转换参数,导致结果中出现'%'字符.
其中第二个%是转换字符.