I want to be able to use a placeholder in a string that already contains a % symbol. For instance, I would like to be able to iterate to open multiple URLs by my URL already contains a % symbol, example url:
http://www.example.com/BLABLA%123BLABLApage=1
To do so, I want to replace the number 1 by a placeholder (%d), but the code seems to be confused by the presence of the % that comes before the placeholder.
解决方案
You can escape % by doubling it:
>>> 'http://www.example.com/BLABLA%%123BLABLApage=%d' % (1,)
'http://www.example.com/BLABLA%123BLABLApage=1'
Alternatively, use str.format() formatting instead:
>>> 'http://www.example.com/BLABLA%123BLABLApage={:d}'.format(1)
'http://www.example.com/BLABLA%123BLABLApage=1'